Reputation: 41
I need to obtain multiple values from a json response and pass those values as a parameter in the header of the next request test step.
Say like I got the response as below:
{ "access_token": "tokenvalue", "token_type": "bearer", "expires_in": 7200, "refresh_token": "" }
Now I need to get the values of token_type
and accesstoken
and pass it as header key 'Authorization' and value as "bearer tokenvalue"
to next test step.
Please let me know how to do the same.
Upvotes: 1
Views: 2587
Reputation: 1
Hi you can do it using java. here is example code.
String name = "username";
String password = "Simple4u!";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
ClientRequest request = new ClientRequest(
"http://localhost:8080/rest123/rest/message/abc?param=Hello");
request.accept("text/plain").header("Authorization", "Basic " + authStringEnc);
ClientResponse<String> response = request.get(String.class);
then those username and password mapped with @HeaderParam automatically Hope this will help you !!!
Upvotes: 0
Reputation: 21
You can do it using groovy script as well
import groovy.json.JsonSlurper
import com.eviware.soapui.support.types.StringToStringMap
responseContent = testRunner.testCase.getTestStepByName("Name of the test step token is included in the responce ").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
access_tok = slurperresponse.data.token
//set parameter values for the next request
def headers = new StringToStringMap()
headers.put("x-auth-token",access_tok)
headers.put("Content-Type","application/json")
headers.put("X-Requested-With","XMLHttpRequest")
testRunner.testCase.getTestStepByName("Name of the test step which need to set the header section with access token").testRequest.setRequestHeaders(headers)
Upvotes: 1
Reputation: 10329
This is a very simple procedure that can be accomplished with absolutely no scripting.
In your resource, where you need the header parameters, create a header parameter called "Authorization". Official documentation on creating header parameters is available.
To transfer the data, for the value use a simple parameter expansion: ${previous_step_name#ResponseAsXml#//*:token_type}+${previous_step_name#ResponseAsXml#//*:access_token}
. Additional documentation on transferring parameters is available.
Upvotes: 0
Reputation: 21389
Here you go.
Add a script assertion to the first test step with the below code(the one where you get the access token and token type).
Note: Provide the appropriate value in the script for nextStepName, headerName variables if they differ from your values. Currently values set are REST Test Request, Authorization respectively. You may ignore editing if you have the same values.
/**
* below is script assertion for the first step and
* will set http headers to the given test step with in
* the same test case user need to pass the test step name,
* and header name if any change
**/
//Modify the name of the test step as needed in your case
def nextStepName = 'REST Test Request'
def headerName = 'Authorization'
//Do not require to edit beyond this
def setHttpHeaders(String stepName, def headers) {
def nextRequest = context.testCase.testSteps[stepName].httpRequest
def existingHeaders = nextRequest.requestHeaders
headers.each {
existingHeaders[it.key] = it.value
}
nextRequest.requestHeaders = existingHeaders
}
def jsonResponse = new net.sf.json.groovy.JsonSlurper().parseText(messageExchange.response.contentAsString)
assert jsonResponse.access_token, "Access token is empty or null"
assert jsonResponse.token_type, "Token type is empty or null"
//creating the header value separated by space between the two
String headerValue = "${jsonResponse.token_type} ${jsonResponse.access_token}"
def headers = [(headerName) : [(headerValue)]]
setHttpHeaders(nextStepName, headers)
Upvotes: 0