content-negotiation for humans
Justin Sheehy - Justin Sheehy - February 06, 2009Simon Willison asked for opinions on how to deliver JSON content properly while also assisting browser-driven exploration and debugging.
Here is a short, simple example of how to use content-negotiation to achieve this.
This is a complete, working webmachine resource with trivial content:
-module(conneg_demo_resource).
-export([init/1, to_json/2, content_types_provided/2]).
-include_lib("webmachine/include/webmachine.hrl").
init([]) -> {ok, x}.
to_json(_,X) -> {"{"key": "value"}n", X}.
content_types_provided(_,X) ->
{[{"application/json", to_json},{"text/plain", to_json}], X}.
A typical request/response, with a tiny bit of header noise trimmed:
$ curl -v http://localhost:8000/js/simonw
> GET /js/simonw HTTP/1.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Vary: Accept
< Server: MochiWeb/1.1 WebMachine/0.20 (There was kicking.)
< Content-Type: application/json
< Content-Length: 17
<
{"key": "value"}
This is what you generally want. The JSON was delivered with the proper content-type and so on. Note the presence of the “Vary” header.
However, if the same request is made with a typical browser’s Accept header, like FireFox’s, the result will be different:
> GET /js/simonw HTTP/1.1
> Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
>
< HTTP/1.1 200 OK
< Vary: Accept
< Server: MochiWeb/1.1 WebMachine/0.20 (There was kicking.)
< Content-Type: text/plain
< Content-Length: 17
<
{"key": "value"}
This time we got the same JSON content, but in text/plain so it will display nicely in a browser window if requested directly. A Web application loading this content would probably set the Accept header in XHR requests to “Accept: application/json” and get the first response.
This is a straightforward use of content-negotiation that serves a useful purpose and (by using Accept properly and providing the Vary response header) still works well with intermediaries and the rest of the mechanics of the Web.
Categories: Blogs Justin Sheehy
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» levicole (Levi Kennedy): @pharkmillups Still seeing it. I might just have to manually install it. The version of Erlang required by Riak is not current version in HB
» quercialwji2 (Quercia Quinn): @Maro7861 http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @RolArens http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @kreese555 http://t.co/pPiIpTCx
» erlang (Andreas Åkre Solberg): RT @SaraJChipps: Node.js is like taking a bubble bath in JavaScript.
» trabajosit (Empleos en IT): argentina Desarrolladores - iOS, Ruby, Erlang: Estamos buscando un líder de desarrollo que quiera hacer una gran… http://t.co/zhTLpGI1
» erlang (Andreas Åkre Solberg): jQuery Scroll Path Plugin http://t.co/DHZ0W36c via @JoelBesada
» martyns (martynas): fighting with load bursts. probably its time to deploy https://t.co/xJxKcRmQ. #erlang
» Louellaoqk (Louella Delaroca): Informationsmanagement in Hochschulen (German Edition): Die Informations- und Kommunikationstechnik (IuK) erlang… http://t.co/95UJGZS3
» fgtrjhyu (アスパラガー): 多分「どうでもいい」だと思うんだ。 Smalltalk→Obj…C erlang→node.jsと同じで。
Statistics
Number of aggregated posts: 10456
Number of comments: 1445
Most recent article: February 06, 2012
Latest comments
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile
» tandblekning easewhite on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: ncomprehensible to me now, but in general, the usefulness and significance is overwhelmingtandblekning easewhite