简体   繁体   English

如何使用 grails 3.3.9 在 RestBuilder 的 post 请求中发送 header

[英]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".当我通过 API 调用发送发布请求时,出现异常“代理票证必须作为标头中的参数发送”。 I don't know what is wrong with the code.我不知道代码有什么问题。 Can anyone help me?谁能帮我?

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM