Search code examples
grailsgroovy

How to send header in post request of RestBuilder using grails 3.3.9


Response response = new RestBuilder.post(finalUrl){
         header: ["ticket", "getProxyTicket()",
                  "name", "abc",
                  "id", "1"
                 ]
        contentType: application/json
        json data
}

I have the above code. And when I send the post request through API call, I get an exception "Proxy ticket must be sent as parameter in header". I don't know what is wrong with the code. Can anyone help me?


Solution

  • There are different ways to do it. This should work:

    new RestBuilder().post(finalUrl){
         // this will work if you want the value
         // of the ticket header to be the literal "getProxyTicket()".
         // if you are trying to invoke a method and you want the return
         // value of that method to be the header value, remove the double
         // quotes around getProxyTicket()
         header "ticket", "getProxyTicket()"
         header "name", "abc"
         header "id", "1"
    
        // no need to set the content type, the 
        // json method below will do that
        // contentType: application/json
        json data
    }