Forum Discussion

GreatCall__Supp's avatar
GreatCall__Supp
New Contributor
10 years ago

Trying to post Header value to another test case

My groovy scripting skills are beginner level, which is giving me this problem.

So I'm using JSONslurper to grab the value for access_token to use as a header value for Authorization in a later test case. Currently, I'm only able to get it to post on the current test case "ObtainAccessToken", which I don't want it to do, and I'm not sure how to get it to post to the other test case (ex. "UseAccessToken").

Also, how do I get it to print additional text like "Bearer" for example, along with the token value? For example, is there a way to get to print like this?
headers["Authorization"] = "Bearer ".slurperresponse.access_token (which I've tried and didn't work)

Here is my groovy code:

import com.eviware.soapui.support.types.StringToStringMap
import groovy.json.JsonSlurper

def request = context.testCase.testSteps["ObtainAccessToken"].testRequest
def headers = request.getRequestHeaders()

responseContent = testRunner.testCase.getTestStepByName("ObtainAccessToken").getPropertyValue("response") //add your property value here
slurperresponse = new JsonSlurper().parseText(responseContent)
headers["Authorization"] = slurperresponse.access_token

request.setRequestHeaders(new StringToStringMap(headers))

4 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    You cant access it on the other testcases because you are getting the token using "context" which is an object that holds information about a validation in-progress, such as the cursor of the content to be validated. Also, I dont think you have to use groovy to get this access token, its much easier to use the point and click functionality(see below), just save the token as a property.

    Point and click functionality details here:
    http://www.soapui.org/Working-with-soap ... sting.html

    Let me know if you have further issues or questions.

    Regards,
    Temil
  • Thank you, I was able to use the point and click feature for my refresh token and use it as a property into a URL parameter, but still can't see how to use the access token as a property into a header value. My access token doesn't have any (body) URL parameters so it has to go in as a header value, if that's possible.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    To add a header value to other testCase create a project level property and add it in header for other testCase request,
    http://www.soapui.org/Scripting-Propert ... properties
    You can get the value of the header using groovy script and then add it in a project property so it can be shared between testcases.

    // Add project property
    testRunner.testCase.testSuite.project.setPropertyValue( "headerval", val)

    Add it in header,
    Check the screenshot

    Thanks,
    Jeshtha