Forum Discussion

rrsqrd's avatar
rrsqrd
New Contributor
11 years ago

setting header value in a rest request

Hi,
I am a c/c++ developer with a few Java courses under my belt and I am new to
webservices/rest/soapUI/groovy/testing etc so please bear with me.
Note that I am using the open source soapUI.

I need to retrieve a value from a rest 'response header field' and then use it in a subsequent rest 'request header field'.
Scenario: loginRequest returns a value in it's response header that I need to extract and use in subsequent requests such as logOut.

I have managed to extract the value from the login response header and am storing it in a property at the test case level
def headerValueFromResponse = testRunner.testCase.testSteps["LoginRequest"].testRequest.response.responseHeaders["NewHeader"][0]
testRunner.testCase.setPropertyValue("NewHeaderProperty", headerValueFromResponse )

I have also manually added the newHeader field to the subsequent request, in this case logOut.
(value is temporarily hard coded)

I can retrieve the property value using an expansion
def expandHeaderProperty = context.expand('${#TestCase#NewHeaderProperty}')

Now I need to be able to use this value and set the respective header field in the logOut request header.
But I haven't had much luck finding anything that 'smells' of setting a header field with code.

Are there methods available that will allow me do this using open source soapUI?

I watched a smartbear video and saw that event handlers were used to do what I am trying to do.
It used sbutmitListener.afterSubmit and sbumitListener.beforeSubmit to dig into getRequest and getResponse methods.
Event handlers don't seem to be available in open source soapUI.
I am wondering if I will need to use soapUI Pro.

thanks in advance.
r2

5 Replies

  • rrsqrd's avatar
    rrsqrd
    New Contributor
    Hi,
    Thanks for the suggestion but the soapUI page on adding headers doesn't have what I'm looking for.

    It shows how I can manually add a 'custom' header field to a request which I have done.
    But I can't set the custom header with a fixed value and then run further tests.
    The value being retrieved from the loginRequest response header is similar to a sessionID in that it will
    be used in the request header of subsequent testCase requests.

    The page also shows how to use property expansion to retrieve the value to be used for the custom header from a property.
    It does not show how to use the property to 'set' the value of the custom header field dynamically through a script
    in a subsequent testCase request. That is why I am asking about method(s) to set a header field.

    thanks
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Go into the SOAP Request step where you want to set the header
    In the request window there's a "Headers" tab at the bottom. Expand it
    Populate the header name, for header value put in your "${#TestCase#NewHeaderProperty}" without the quotes

    Thanks,
    Michael Giller
    SmartBear Software
  • I can retrieve the property value using an expansion
    def expandHeaderProperty = context.expand('${#TestCase#NewHeaderProperty}')
  • rrsqrd's avatar
    rrsqrd
    New Contributor
    Yes, I may have watched the same video.
    EventHandlers seems the best way to go to set header values via code.

    I have code that adds a custom header field to a request which I've tested in a testStep.
    Unfortunately, I couldn't get the EventHandler to work as the target value I specified gives an exception:
    java.lang.NullPointerException
    at com.eviware.soapui.model.propertyexpansion.resolvers.SubmitPropertyResolver.resolveProperty

    I used ${#TestStep#DeleteUserRequest} for the target where “DeleteUserRequest” is the
    name of an HTTP Test Request test step.

    I have been waiting to hear back on the exception issue for quite some time.
    Maybe posting this here help..

    r2