4

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. I'm 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
PUT /ressource/ID?format=xml

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/resource/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.

1
  • As you are creating a new resource, you should use POST instead. PUT is more about replacing the resource at a fixed location, where the client knows the URI, it seems very broken to allow the client to say what ID to use to store a resource. POST would have the user use a URI like /resource and the server will respond with the URI that can be used to access the new 'thing', like /resource/666 Commented Jul 22, 2015 at 12:27

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
This is about the Content-Type of a request body. I can't think of any situation where a person would want to specify this and not be able to. If you're submitting a web form, for example, it's not going to be JSON, so this wouldn't be relevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.