grails-plugins / grails-rest-client-builder

REST client plugin that uses Spring's RestTemplate
http://grails.org/plugin/rest-client-builder
Apache License 2.0
65 stars 32 forks source link

Issue passing custom header values #10

Open mohan-krishnan opened 11 years ago

mohan-krishnan commented 11 years ago

I am using the latest grails (2.1.2) with rest-client-builder plugin version 1.0.3. I am trying to pass in a custom request header value (say SEC_USER) to the target server which pre-authenticates a request with this request header value. I am using the following code to pass the value in but I dont get the header value in the receiving end.

RestBuilder rest = new RestBuilder(connectTimeout:5000, readTimeout:20000); 
rest.get("<some uri>") { 
    contentType MediaType.APPLICATION_JSON.toString() 
    header("SEC_USER", "foo") 
} 

Looking at RequestCustomizer in the RestBuilder class the header() method should add a named header to the HttpHeaders field as it calls headers[name] = value

Could this be an issue in the RestTemplate class (method exchange(...)) which this plugin uses internally?

thu-nguyen commented 11 years ago

I've tested it again and found that you don't get the header value because the header() method isn't called. The reason is the name of method header(), I don't know what it has special meaning but when I changed the name, it works. You can change and check again:

In RequestCustomizer class change the method header to headers

RequestCustomizer headers(String name, String value) { headers[name] = value return this }

And then using with new name RestBuilder rest = new RestBuilder(connectTimeout:5000, readTimeout:20000); rest.get("") { contentType MediaType.APPLICATION_JSON.toString() headers ("SEC_USER", "foo") }

graemerocher commented 11 years ago

This issue is because you're using this from a controller, so instead of calling the header method of the RestBuilder it calls the header method of the controller. The workaround is to do:

delegate.header("foo", "bar")

or

headers["foo"] = "bar"