3
votes

I am new to jmeter. Can anyone help me to use a response object of one request to be passed as a request header of next HTTP request ?

Let me explain.

  1. I am getting an access token along with the response of login in my app:

    {: "responseCode":18, : "message":"Successfully logged in.", : "responseObject":"8zWExE4eSdhcJDwnW9MgIw=="}

  2. No I want to use this access token (8zWExE4eSdhcJDwnW9MgIw) as one of the parameter of next request.

I used JSON Path Extractor for this.But its not working.

3
But I have already tried using $.responseObject and $.responseObject[0] and its not working. Lemme explain what I have tried in this scenario.fms
These are the components i have implemented in my test script. 1 HTTP Request Defaults 2 HTTP Cookie Manager 3. User Defined Variables (Inside this a variable is declared as "access_Token" and its value as "jsonVal", which is the JSON Path Extractor Name) 4. HTTP Request (Login) (This request is giving the response as { : "responseCode":18, : "message":"Successfully logged in.", : "responseObject":"NXVQR/bQBGJV4ugJOgpQeg==" } ) 4.1 HTTP Header Manager 4.2 JSON Path Extractor (Extractor name is jsonVal, which i have passed in User defined variables)fms
5 HTTP Request (Reservation) its request body is like { "eventId":4446, "buyerId":10, "invCount":1, "eventPriceId":169 } 5.1 HTTP Header Manager Accept application/json Content-Type application/json accessToken ${access_Token} 6 Result Tree Response of Reservation Request is { : "responseCode":100, : "message":"Invalid Access Token." }fms

3 Answers

3
votes

I am using JSON Path Extractor as well and it works great if it is properly configured. Just put it into request and fill fields:

  • Variable Name: access_token (or any other you want to use later in request like this ${access_token})
  • JSON Path: responseObject should be enough if the JSON you pasted is full response (thjose additional colons are just some mistakes when copy-pasting or the JSON is corrupted?)
  • Default Value: I always use some value like 'NotUpdated!' here so I can assert in the next step or at least see it easily in request.
2
votes

Not seeing the full response it is quote hard to come up with a correct JSON Path expression.

Given what you posted it should be something like $.responseObject

See JSON Path Syntax guide for queries syntax examples and Using the XPath Extractor in JMeter (scroll down to "Parsing JSON") for plugin installation instructions and XPath to JSON syntax mapping.

1
votes

Just in case anybody would face an issue with multiple variable extractions using JSON Extractor (like me), make sure to:

  • List the names of variables/path expressions/default values using a semicolon as a separator.
  • Provide default values for every variable.

The second point is apparently required, and I only found out about it from this Medium post.