Forum Discussion

mgroen2's avatar
mgroen2
Super Contributor
8 years ago

how to assign property value (of a response) to a variable, and re-use it in a new request?

I am new to SoapUI, and currently testing a Rest API.

After logging in, I receive an accesstoken in the response.

I need this accesstoken to be pasted in a new request (to pass authorization). I have read info on Property Transfer, but it's not easy :(

is there a possibility to assign the value of accesstoken, to a variable (lets say var1), and paste that in the requests in some easy way?

 

7 Replies

    • nmrao's avatar
      nmrao
      Champion Level 3
      Looks this is related to this other thread of yours. Have you tried with script provided there?

      You haven't mentioned where you need to use the extracted value, by the way?
      https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-assert-on-value-in-a-Json-response/m-p/139172#U139172
      It is even not sure what happened to other question as it is still open while many relevant answers were provided. If you found helpful answer or solved altogether please convey that. No response to the solutions provided to your earlier questions.

      Regarding this question: you need to add another statement to script provided in the above thread.

      context.testCase.setPropertyValue('TOKEN', parsedJson.accessToken.toString())

      In order to access the TOKEN in other request steps, use property expansion ie., ${#TestCase#TOKEN}
  • Does the accessToken value need to be entered as part of the request XML or as one of the soapui properties?

     

    If it needs to be part of the XML, are you not able to pass a tokenised value through i.e.

     

    <Test>${Login 1 - Portal Login#Request#//accessToken[1]}</Test>

     

    For example purposes:

     

    My Login 1 - Portal Login request looks like this:

     

    <accessToken>1234</accessToken>

     

    so the Test element should be replaced with 1234 upon executing the second request.

    • mgroen2's avatar
      mgroen2
      Super Contributor

      simonjdonnelly wrote:

      Does the accessToken value need to be entered as part of the request XML or as one of the soapui properties?

       

      If it needs to be part of the XML, are you not able to pass a tokenised value through i.e.

       

      <Test>${Login 1 - Portal Login#Request#//accessToken[1]}</Test>

       

      For example purposes:

       

      My Login 1 - Portal Login request looks like this:

       

      <accessToken>1234</accessToken>

       

      so the Test element should be replaced with 1234 upon executing the second request.


      simonjdonnellythe response
      of the first request is this:
      I need to have the value for accesstoken be pasted in subsequent requests. But I can't do it using Property Transfer.
      • simonjdonnelly's avatar
        simonjdonnelly
        Contributor

        Passing the value through as a parameterised version should work then:

         

        I don't have any sample REST JSON services to hand, so I've used soap but it shouldn't be too different:

         

        Place ${Login 1 - Portal Login#Request#//Response[1]/accessToken[1]} in the second request, where you want the access token previously generated to appear.

         

        This will take the value from the test step named 'Login 1 - Portal Login' and take the accessToken from the structure Response > accessToken.