I am new in JMeter. I am trying to set access token dynamically through "Regular Expression Extractor".
Basically I am trying to first login and then from it's response , I am trying to get access token and set it in "CreatePost" API.
I am able to get access token using #1 Login API where response is as below.
{
"Message": "Login successfully.",
"Status": "Success",
"HttpStatus": 200,
"Data": {
"token_type": "Bearer",
"expires_in": 10000,
"access_token": "eyJ0eXAiE",
"refresh_token": "2bcf1f455f2",
"name": "jmeter_test",
}
}
Could you please help me to get access_token from above response.
To get this , I have created "Regular Expression Extractor " and set
Name of Createdvariable : access_token,
Regular Expression: "access_token": "value"
Template $1$
Match No. 1
Could yo please check the regular expression?
Thanks in advance.
Use JSON Extractor as a child of the Sampler Returning Response with following JSON Path Expression:
$.Data.access_token
If you really need to use the regular expression extractor you need to amend your regular expression to look like:
"access_token":\s+"(\w+)"
where:
(\w+) matches any number of aplhanumeric characters
\s+ matches optional whitespace character
Demo:
More information:
JMeter: Regular Expressions
Perl 5 Regex Cheat sheet
However for JSON responses it makes sense to use JSON Extractor which allows executing arbitrary JsonPath queries to get "interesting" data from the JSON responses, the relevant JsonPath expression will be:
$..access_token
where:
.. is "deep scan" operator, it will find the search term anywhere in the data
access_token - the JSON attribute you're looking for
More information: API Testing With JMeter and the JSON Extractor
Related
I am getting response as
{
"statusCode": 219,
"body": "{"message": "otp sent to user", "session": "f603ee03-4906-4451-b4e5-b19f0ccf84d0", "otp": "123456"}"
}
I want to get the Session number from this response. Do we have any suggestions
I tried post processors however couldn't as the response is in string
This is really strange response. It looks like valid json but isnt really. You can try with regular expression extractor with this expression to match the UUID:
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
If the response really looks exactly how you're posting it - you won't be able to use JSON Extractor because it's not a valid JSON
You can use either Regular Expression Extractor and the regex like:
session"\s?:\s?"(.+?)"
If you want explanation:
\s? - optional whitespace character
() - grouping
. - any character
+ - repetition
? - don't be greedy, stop at first match
Or Boundary Extractor configured like:
More information: The Boundary Extractor vs. the Regular Expression Extractor in JMeter
I am getting a response in form of serialized json format for an api request as below
{"Data":"{\"orderId\":null,\"Tokens\":{\"Key\":\"abcdefgh123456\",\"Txnid\":\"test_5950\"}","success":true,"Test":"success"}
I want to extract Key value in Jmeter and I have to use into next request. Can someone help me on extracting the value?
Your JSON seems incorrect. The valid JSON should be like:
{
"Data":{
"orderId":null,
"Tokens":{
"Key":"abcdefgh123456",
"Txnid":"test_5950"
},
"success":true,
"Test":"success"
}
}
Add a JSON Extractor to the request from where you want to extract the Key value.
assign a variable name, i.e key
JSON Path Expression will be : .Data.Tokens.Key
use the extracted value as ${key} into the next request.
If your JSON really looks exactly like you posted the most suitable Post-Processor would be Regular Expression Extractor
The relevant regular expression would be something like:
"Key"?\s*:?\s*"(\w+)"
where:
``?\s*` - arbitrary number of whitespaces (just in case)
\w - matches "word" character (alphanumeric plus underscores)
+ - repetition
() - grouping
More information:
Using RegEx (Regular Expression Extractor) with JMeter
Perl 5 Regex Cheat sheet
JMeter: Regular Expressions
i'am using jmeter "Regular Expression Extractor",my response looks as below
[{"#class":"com.test.dto.BoardDTO",
"ReadOnly":false,
"author":"John",
"id":"89BC331D723F",
"isPublic":false
},
{"#class":"com.test.dto.BoardDTO",
"ReadOnly":false,
"author":"Alex",
"id":"FTH7JBDRF567",
"Public":false
}]
I need to extract all IDs of class:"com.test.dto.BoardDTO" in this case "89BC331D723F" and "FTH7JBDRF567"
Any proposition please !
You should use JSON Extractor instead of regular expression extractor
Add a JSON Extractor and fill in the fields as below
JSON path expression: $.[*][?(#.#class == "com.test.dto.BoardDTO")].id
Match Numbers: -1
This will return all IDs where #class value was com.test.dto.BoardDTO. You can validate it using View Results Tree & Debug Sampler combination.
I have a dynamic value in response but it keep on changing the position. How can i capture it.
ex:1st iteration value is 2nd position
2nd iteration value is 4th position
3rd iteration value is 1st position...like that
Can you some one please guide me how to capture this value using Regular Expression Extractor or any other Extractor.
Perhaps Regular Expression Extractor is not the best choice, especially when it comes to HTML response type so make sure to use the suitable Post-Processor, i.e.
HTML - CSS Selector Extractor
XML - XPath Extractor
JSON - JSON Extractor
Anything else: Boundary Extractor might be easier to use than the Regular Expression Extractor
Going forward include (at least partial) response in your question and indicate which value you want to extract so we could come up with the most efficient approach.
As of JMeter 3.0, it’s far easier to extract data from JSON responses using the JSON variable extractor. JSON is an extremely simple data format which has taken over XML a few years ago.
An increasing number of REST APIs and servers, are using JSON as their primary data exchange format. Here, we will use JMeter to parse the JSON response.
Suppose we have a JSON response as follows:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
To parse the above JSON with JMeter, we need to add the JSON Extractor to our test plan.
Right click on Test Plan –> Add –> Post Processors –> JSON Extractor
jmeter json extractor parser
Now, we should see the following view:
json extractor jmeter
In the JSON Path expressions field, we can insert our JSON path to parse the JSON response
Here are some example Json Path expressions that can be used to extract data from the Json document exposed above:
JSONPATH RESULT
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[2:] Book number two from tail
$..book[?(#.isbn)] All books with an ISBN number
$.store.book[?(#.price < 10)] All books in store cheaper than 10
$..book[?(#.price <= $[‘expensive’])] All books in store that are not “expensive”
$..book[?(#.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books
I am working on a POST service that give JSON response.
I have to extract certain value from the JSON response. example-
`{
"Result":
{ "Id":22
"StartTime":
"EndTime":
"RoutePoints":
[{ "Id":675,
}
{ "Id":676,
}
]
}
} `
My first part of the question-
How do I refer the "Id" variable inside the "RoutePoint" array using regular expression extractor? I can simply use "Id", but I also have an "Id" variable outside the "RoutePoint" array.
Secondly-
How do I take the "Id" each time and run them in a loop in the following service? Example- I take "Id=675" and perform a job, then take "Id=676" and perform that same job. Please be as detailed as possible, I am new to JMeter.
I would recommend going for JSON Path PostProcessor which is available since JMeter 3.0
Add JSON Path PostProcessor as a child of the request which returns above JSON and configure it as follows:
Variable Names: anything meaningful, i.e. Id
JSON Path Expressions: $..RoutePoints.*.Id
Match Numbers: -1
You should get variables like:
Id_1=675
Id_2=676
Id_matchNr=2
suitable for iteration with i.e. ForEach Controller
Demo:
References:
JSONPath - XPath for JSON
Advanced Usage of the JSON Path Extractor in JMeter