Forum Discussion

bkbn16's avatar
bkbn16
Contributor
9 years ago
Solved

Need to update/set Header Values through groovy scripting

Hi,

 

I have created a REST Mock response. To invoke the response; I have the URI and request body. 

 

def Accept = mockRequest.getRequest().getHeader("Accept") 
log.info(Accept)

 

This is giving me /* as response in console, but I need to update it as "application/json"

 

def Method = mockRequest.getRequest()

log.info(Method)

 

This is giving me ...

POST rest/login

Accept: /*

Content-Type: application/json

Content-Length: 8

......

 

But what I want is along with the request body like

POST rest/login

Accept: application/json

Content-Type: application/json

Content-Length: [length of this message]

{'username':'admin', 'password': 'admin'}

 

How can I update or set the request header and the body through the script ?

5 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Is this the sort of thing you mean i.e. to set the mock's response Accept header using Groovy in a mock despatch script:

    def headers = mockResponse.responseHeaders
    headers["Accept"]=["application/json"]
    mockResponse.responseHeaders=headers

    ?

     

    Regards,

    Rupert

     

     

    • bkbn16's avatar
      bkbn16
      Contributor

      Similar to what you have posted. What you showed me is the response, but what I need is in the request section.

       

      I am getting this as request body ... 

       

      POST rest/login

      Accept: /*

      Content-Type: application/json

      Content-Length: 8

       

      but I need it like this ...

       

      POST rest/login

      Accept: application/json

      Content-Type: application/json

      Content-Length: [length of this message]

      {'username':'admin', 'password': 'admin'}

       

      AND POST IT AS REQUEST BODY. WOULD THIS BE POSSIBLE ?

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        OK, I think I must have got the wrong take on what you are trying to do.

         

        So, you say you are creating a mock response... I took this as creating a response for a standard SoapUI REST Mock?

         

        Where you want to add the Accept header using groovy, do you instead mean that you want to use SoapUI and Groovy to make a REST request to a mock and want to add the Accept header using Groovy? 

         

        Please explain a little more of what you are trying to do.

         

        Thanks,

        Rupert