Forum Discussion

richie's avatar
Community Hero
5 years ago

I need to grab some cookies for later use....



This new thing I'm testing has some new OAuth security and the first thing I need to do is grab some cookies from the request.


I've never done this before - I know - weird - right?


Anyway - I have a blank payload but plenty of headers

There are 4 headers entitled Set-Cookie and each of these 4 'Set-Cookie' headers have different values - please see screenshot below



Typically if I need to capture header values for later use - I have a bit of groovy to do this (as follows:)

//Takes one of the elements of the response Header
def value = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders["headername"]

//Read this value into a parameter - writes the header value into the Properties test step
def groovyUtils = new context )
groovyUtils.setPropertyValue("Properties", "headername",value[0])

HOWEVER - the above is never going to work - I need to capture each of the 4 'Set-Cookie' values and they each have the same name?!??!


Can anyone please advise how I can grab these cookies?


Many thanks!










  • That was just for my testing -- you would just replace that with:


    testRunner.testCase.testSteps["GET Cookies"].testRequest.response.responseHeaders["Set-Cookie"]

14 Replies

  • You may have already figured it out, but it looks like each value has a unique identifier you could use. If you need to reference each one by name, you could do something like this:


    def headers = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders
    headers.each {
    if (it.getKey() == 'Set-Cookie') { // assuming this is a map
    def valueParts = it.getValue().split('=') // Split the value on the equals
    testRunner.testCase.setPropertyValue(valueParts[0], it.getValue())

    // This would get you the following test case properties:
    testCase.getPropertyValue('returnurl') // returnurl=9d97d277ed656a2...
    testCase.getPropertyValue('authtype') // authtype=72b76f4f5cf...
    testCase.getPropertyValue('state') // state=81ef1f0ff48fcd...
    testCase.getPropertyValue('nonce') // nonce=7511a89aaacda...


    Or, if you just need them all and don't need to reference each one by name, you could just throw them in a list:

    import groovy.json.*

    def headers = testRunner.testCase.testSteps["REST Request)"].testRequest.response.responseHeaders def cookieList = [] headers.each { if (it.getKey() == 'Set-Cookie') { // assuming this is a map cookieList.add(it.getValue()) } }

    // This would get you: ["returnurl=9d97d277ed656a2", "authtype=72b76f4f5cf", "state=81ef1f0ff48fcd", "nonce=7511a89aaacda"]

    // Store as test case property -- can only store String, that's why we serialize the List to JSON
    testRunner.testCase.setPropertyValue("cookieList", new JsonBuilder(cookieList).toString())

    // To use in another Groovy script
    String cookieListStr = testRunner.testCase.getPropertyValue("cookieList")
    def cookieList = new JsonSlurper().parseText(cookieListStr) // Deserialize JSON back into a List


    • richie's avatar
      Community Hero

      Hey JustinM89 


      I got stuck on something else - but I'm circling back to this tomorrow - so I'll be looking at it then - thanks so much for your help - its far more than I would've expected!





      • richie's avatar
        Community Hero



        I haven't forgotten about this - I'm still working on this and other things, so cant close off the ticket as yet,





  • richie's avatar
    Community Hero



    My bad - I found a post by nmrao  from way back answering this (link) - I'm trying to tailor the script accordingly to exactly what I need -  so please don't anyone waste any time looking at this - I "might" be able to sort myself out - I'll come back with either questions or to close off the ticket.







    • Olga_T's avatar
      SmartBear Alumni (Retired)

      Thanks for sharing the link, richie !
      We are looking forward to your updates.