05 January 2007

Setting the Accept header

Our site is running on edge rails and we are using the new REST interface. The site also uses a lot of Ajax to make requests that only accept XML as a response. Setting the accept header turned out to be very easy…Just add the header information to the third parameter of the request, which is a hash of HTTP headers.

put "/bookshelf/1/sections/5",
{ :lp => 0,
:pvW => 300,
"section[title]" => "New title",
"section[body]" => "new very short body" },
{ "Accept" => "text/xml" }

In this example I invoke the sections_controller’s update method because of the put call and I will receive my response in XML because I set the Accept header to “text/xml”. Now that I understand this, I can write integration tests for the rest of the application.

No comments: