tags:

views:

2673

answers:

2

I have a REST data service where I want to allow the users to create new items with HTTP PUT using different formats like json,xml,csv. Now I'm a little unsure how to best handle the format specification in the url:

PUT ->   /ressource/ID/json
PUT ->   /ressource/ID/xml

or

PUT ->   /ressource/ID?format=json

So what is the best way to specify a format indicator?

If I specify the format with an query parameter and want to do a PUT how can I do this with curl?

curl -T test/data.json -d "format=json"  http://localhost:5000/ressource/33

does not work.

curl -T test/data.json http://localhost:5000/update?format=json

works, but I would rather let curl build the query parameters instead of adding them by myself.

A: 

In rails, they make the format part of the url routing in a familiar way.

PUT /resource/id.json
PUT /resource/id.xml
  • Note: in rails this indicates the expected response format, not the data format, not that that matters to your own application.
Nick Retallack
The problem with this is that you then have different URIs for the same resource - I believe this violates a constraint of REST.
Wahnfrieden
+7  A: 

A general principle of RESTful web services is to use the features built-in to HTTP, when applicable. In this case, you can indicate the format of your PUT request's content by setting the Content-Type header to application/json or application/xml.

Peter Hilton
Good idea :D. For the record, I like your solution better than mine.
Nick Retallack
Thanks! That's the solution I was searching. And with curl --header it is easy and clean to secify too.
Peter Hoffmann
Unfortunately if you want to make this an open API, many people will be unable to use Content-Type headers. The sad reality is that you usually need to allow some hacks, even if you permit the standardized way too.
Wahnfrieden