Forum Discussion

bkbn16's avatar
bkbn16
Contributor
8 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 ?

  • bkbn16's avatar
    bkbn16
    8 years ago

    Hi rupert,

     

    Thanks for replying back again. This is how I need to do ...

     

    I first created a Empty Project > Mock Service. Underneath this, created a Mock action 'POST' (resource path being: /rest/login). Underneath this, a JSON mock response added. So when I run the mock service with resource path I get the json mock response directly. Now, in addition to the resource path I am trying to add a request body (also in Json), something like this {"username": "admin", "password": "admin"}. 

    Then, I wrote a script which must alter the Accept Header Field in request body, as well as append the json message to the request body and calculate the Content-Length the request body.

     

    import java.lang.*
    import java.util.*
    import java.io.*
    import java.net.*
    import groovy.lang.*
    import groovy.util.*
    import javax.servlet.http.*
    def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
    import groovy.json.JsonSlurper
    import com.eviware.soapui.model.iface.*
    log.info("response="+mockRequest)
    def query = mockRequest.getRequest()
    //log.info(query)

    def ContentType = mockRequest.getRequest().getHeader("Content-Type").toString()
    //log.info(ContentType)
    def ContentLength = mockRequest.getRequest().getHeader("Content-Length").toString()
    //log.info(ContentLength)
    def Accept = mockRequest.getRequest().getHeader("Accept").toString()
    //log.info(Accept)
    def method = mockRequest.getMethod().toString()
    //log.info(method)
    def path = mockRequest.getPath().toString()
    //log.info(path)

    jsonAsText = '''{ "username": admin, "password": admin, "device-token": AA}'''
    //log.info(jsonAsText)

    def RequestBody = method +" " +path + '\n' + "Content-Type: "+ContentType + '\n' + "Accept: "+Accept + '\n' + "Content-Length: "+ContentLength + '\n' + '\n' + jsonAsText
    log.info(RequestBody)

    RequestBody.setRequestContent
    return "JSON"

     

    How do I alter the request body before returning the JSON response ? Thanks in advance.

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