Reputation: 11
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 the header() method should set 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?
Upvotes: 1
Views: 2296
Reputation: 101
Grails enhances controller classes with a dynamic method:
header(Object instance, String headerName, Object headerValue)
Depending on where you're using your builder, this is likely being called instead of actually passing the header()
block to the builder. When I put the code above into a controller I saw this behavior. One way to solve this would be to move closure into a class in src/groovy
. For example, when I changed the invoke code to
def check() {
RestBuilder rest = new RestBuilder(connectTimeout:5000, readTimeout:20000);
rest.get("http://localhost:8080/so13789857/hello", Thing.getRestBuilderClosure())
}
And the closure to come from src/groovy/so/Thing.groovy
static Closure getRestBuilderClosure() {
return {
contentType MediaType.APPLICATION_JSON.toString()
header("SEC_USER", "foo")
}
}
It solved the issue.
Upvotes: 4