asgard icon indicating copy to clipboard operation
asgard copied to clipboard

Content Negotiation

Open mkrogemann opened this issue 10 years ago • 2 comments

I use RestClient (Ruby Gem) to request a loadbalancer resource:

RestClient.get "http://localhost:8080/loadBalancer/show?name=xyz-stage-b3502bda&region=us-east-1", "Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate"

I would expect to have json returned. But: The only way I have found to get json for this resource is to use the .json extension in the URL and I would like to avoid having to do this.

I doubt that RestClient is the culprit here but if other users do not see the same behavior I will try with another client.

In fact right now I tried with curl and get the same result: HTML instead of json. Any ideas?

mkrogemann avatar Jul 25 '13 17:07 mkrogemann

I see what you mean. The reason has to do with Grails content negotiation techniques, the fact that browsers might request XML by default when you really want HTML, and the fact that Asgard's endpoints are currently multi-purpose; the same Grails controller and action can serve data for either a human or a machine to read.

http://grails.org/doc/latest/guide/single.html#contentNegotiation

Here are three solutions for you to consider.

Change Config.groovy

One solution would be to add the following line to ~/.asgard/Config.groovy or to the Config.groovy file in Asgard's source code:

grails.mime.use.accept.header = true

According to the Grails documentation, the caveat here is that some browsers are known to request text/xml when they ought to request text/html, including Firefox. However, I just tried the latest Firefox and found that it requests text/html as it should, so the Grails default setting might be outdated. Feel free to add the above line of configuration code to your ~/.asgard/Config.groovy and restart Asgard and see if you're happy with the result.

Rearrange URL to put file type at end

If you'd rather not do that, you can rearrange the URL so the .json will go on the end. Instead of http://localhost:8080/loadBalancer/show?name=xyz-stage-b3502bda&region=us-east-1 try http://localhost:8080/us-east-1/loadBalancer/show/xyz-stage-b3502bda

Then it's less trouble to append .json http://localhost:8080/us-east-1/loadBalancer/show/xyz-stage-b3502bda.json

Request Parameter "format"

A very simple solution is to add a regular request parameter called format to your URLs. http://localhost:8080/loadBalancer/show?name=xyz-stage-b3502bda&region=us-east-1&format=json

joesondow avatar Jul 26 '13 00:07 joesondow

I would prefer to activate the Grails config setting that you mentioned, but there may indeed be trouble ahead as this resource suggests (albeit from 2009): http://www.gethifi.com/blog/browser-rest-http-accept-headers I will try locally with Chrome and Safari on Mac, let's see...

mkrogemann avatar Jul 30 '13 17:07 mkrogemann