Forum Discussion

Niv12's avatar
Niv12
New Contributor
7 years ago
Solved

Is there anyway to add new header?

Is there anyway to add new Headers dynamically .

If i use the below code, it is resetting all headers . I want to add new parameter to the header , without modifying existing ones.

Is that possible?

 

def headers = new StringToStringMap()
headers.put("Authorization","Basic jXXXXXXX")
testStep.testRequest.setRequestHeaders(headers)

  • Sure.

     

     

    import com.eviware.soapui.support.types.StringToStringsMap
    StringToStringsMap someHeader = ["Authorization": ["Basic jXXXXXXX"]]
    testStep.testRequest.requestHeaders += someHeader

    Above is just to explain how everything is typed. But all you really need is this:

     

    testStep.testRequest.requestHeaders += ["Authorization": ["Basic jXXXXXXX"]]

    Notice that it is a StringToStringsMap (the second Strings is plural), so you need to get it as a list of strings, even if there's only one. Here's how to add two:

     

    testStep.testRequest.requestHeaders += ["Set-Cookie": ["firstName=john", "lastName=smith"]]

    And here's how to add one if there is already a header with the same name:

     

    def oldCookies = testStep.testRequest.requestHeaders["Set-Cookie"]
    testStep.testRequest.requestHeaders += ["Set-Cookie": oldCookies + "middleName=Gustav"]

    Or alternatively:

    def copyOfHeaders = testStep.testRequest.requestHeaders
    copyOfHeaders["Set-Cookie"] += "middleName=Gustav"
    testStep.testRequest.requestHeaders = copyOfHeaders

    One last thing - if doing a Basic authentication is your only requirement, you can alternatively just set the details in the Authentication tab in the editor for your TestRequest.

2 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    Sure.

     

     

    import com.eviware.soapui.support.types.StringToStringsMap
    StringToStringsMap someHeader = ["Authorization": ["Basic jXXXXXXX"]]
    testStep.testRequest.requestHeaders += someHeader

    Above is just to explain how everything is typed. But all you really need is this:

     

    testStep.testRequest.requestHeaders += ["Authorization": ["Basic jXXXXXXX"]]

    Notice that it is a StringToStringsMap (the second Strings is plural), so you need to get it as a list of strings, even if there's only one. Here's how to add two:

     

    testStep.testRequest.requestHeaders += ["Set-Cookie": ["firstName=john", "lastName=smith"]]

    And here's how to add one if there is already a header with the same name:

     

    def oldCookies = testStep.testRequest.requestHeaders["Set-Cookie"]
    testStep.testRequest.requestHeaders += ["Set-Cookie": oldCookies + "middleName=Gustav"]

    Or alternatively:

    def copyOfHeaders = testStep.testRequest.requestHeaders
    copyOfHeaders["Set-Cookie"] += "middleName=Gustav"
    testStep.testRequest.requestHeaders = copyOfHeaders

    One last thing - if doing a Basic authentication is your only requirement, you can alternatively just set the details in the Authentication tab in the editor for your TestRequest.