Forum Discussion

thakurnilesh8's avatar
thakurnilesh8
New Contributor
9 years ago

How to set Session Cookie ?

There is this Login and Logout operations and there are many other operations in the web service that I am trying to set. With the login operation I am able to generate the session id and the session cookie. However, to invoke other operations I need to use the same session cookie. How does I go about using the same cookie as generated in the login operation ?

4 Replies

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Thakurnilesh8,

     

    It sounds like you are using either SoapUI or Ready! API, right? This forum is for TestComplete users. Please let me know which SmartBear product you are working with – I’ll move your question to the correct audience.

    • thakurnilesh8's avatar
      thakurnilesh8
      New Contributor

      I am using SOAPUI 5.0.0. 

      My apologies for posting it in the wrong forum. It will be really helpful if you could post it in the correct forum. 

       

      Thank you 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Here is how you need to do it.

    1. Extract the required value from the response of login request, can be as apart of Script assertion
    2. Set it as header for the next request, may be other request which needs to be transacted including logout as you mentioned.

    Script Assertion body goes as below (of course, above two are done in this single script only)

     

     

    /**Below script should be used as script assertion for login request step
    * Assumes below
    * a. login response contains http header called 'Set-Cookie'
    * b. other request needs to send http header called 'Cookie'
    * In case if there is any change in able two you may need to change its references below
    **/
    def responseCookieKey = 'Set-Cookie'
    def requestCookieKey = 'Cookie'
    def setHttpHeaders(String nextStepName, def headers) {
        def nextRequest = context.testCase.testSteps[nextStepName].httpRequest
        def existingHeaders = nextRequest.requestHeaders
        headers.each {
            existingHeaders[it.key] = it.value
        }
        nextRequest.requestHeaders = existingHeaders
    }
    
    
    if (messageExchange.responseHeaders.containsKey(responseCookieKey)) {
      log.info "Found Cookie in the response headers"
      def cookiez = messageExchange.responseHeaders[responseCookieKey]
      assert null != cookiez, "Response does not contain Cookie"
      def nStepName = context.testCase.testStepList[context.currentStepIndex + 1].name
      def headers = [(requestCookieKey) : (cookiez)]
      setHttpHeaders(nStepName, headers)
    } else {
      log.error "Not Found Cookie in the response headers"
    }