Forum Discussion

syshen's avatar
14 years ago

Pass cookie value to another request

Hi

I am working on an service, which is using RESTful APIs. To access the API, I need to sign-in first to get the session token, which is returned in the "Set-Cookie" header of the sign-in response, and then pass this session token in the API's request header.
I am able to create two steps in the testsuite, one is to HTTP POST to sign in and get response, and another one is to request for RESTful API. But I don't know how to extract the session token value out from the sign-in response, and pass it to the request of API.
Is there any way to do that in soapUI?
  • Please see this Groovy script.
    ---
    // You can get http header of "Set-Cookie" from "login someone" test step in soapUI
    def setCookie = testRunner.testCase.testSteps["login someone"].testRequest.response.responseHeaders["Set-Cookie"]

    // if you want to get JSESSIONID=value, use this regular expression
    re = /(JSESSIONID=[A-Za-z0-9]+)/
    matcher = ( setCookie =~ re )
    def jsesid = matcher[0][0]

    // confirm the JSESSIONID with log.info
    log.info "$jsesid"

    // Set the ID to TestCase cookie property
    def tc = testRunner.testCase.testSuite.getTestCaseByName("sample testcase")
    tc.setPropertyValue("cookie","$jsesid")
    ---

    TestCase property is able to use a http cookie header's value.
    Enter "${#TestCase#cookie}" to "Value" and "Cookie" to "Header" in a Test Step's "Header" part.

    Please try it. Thank you.
  • awsgamdocs's avatar
    awsgamdocs
    Occasional Contributor
    akeo wrote:
    Please see this Groovy script.
    ---
    // You can get http header of "Set-Cookie" from "login someone" test step in soapUI
    def setCookie = testRunner.testCase.testSteps["login someone"].testRequest.response.responseHeaders["Set-Cookie"]

    // if you want to get JSESSIONID=value, use this regular expression
    re = /(JSESSIONID=[A-Za-z0-9]+)/
    matcher = ( setCookie =~ re )
    def jsesid = matcher[0][0]

    // confirm the JSESSIONID with log.info
    log.info "$jsesid"

    // Set the ID to TestCase cookie property
    def tc = testRunner.testCase.testSuite.getTestCaseByName("sample testcase")
    tc.setPropertyValue("cookie","$jsesid")
    ---

    TestCase property is able to use a http cookie header's value.
    Enter "${#TestCase#cookie}" to "Value" and "Cookie" to "Header" in a Test Step's "Header" part.

    Please try it. Thank you.


    Hi,
    I need to get the token from Login REST then to add it as a header with name: "authorization" and value: "the actual token we get"

    By using what you suggested I was able to see the token:-
    def setCookie = context.testCase.testSteps["/smb/Login - Request"].testRequest.response.responseHeaders["uxfauthorization"]

    My question is how can I transfer it to the next REST's request as header?
    I appreciate the help.
    BTW, you can see my full POST here:-
    viewtopic.php?f=2&t=23132