M
M
Maxim2015-11-07 16:58:25
Java
Maxim, 2015-11-07 16:58:25

How to correctly specify the response type for the REST API?

Drinking restful service in Jersey. One of the methods returns a list of (for example) employees in one of the formats (xml, json, binary etc).
Which method of forming the resource name is more correct to choose, given the chosen format?
With content-type:

-X GET -H "Content-Type:text/xml" -G http://super-rest/employees
-X GET -H "Content-Type:application/json" -G http://super-rest/employees
-X GET -H "Content-Type:application/octet-stream" -G http://super-rest/employees

Or with a simple query parameter:
-X -G http://super-rest/employees?type=xml
-X -G http://super-rest/employees?type=json
-X -G http://super-rest/employees?type=binary

?
Is it correct to specify in the Content-Type of the request not the content type of the request (I don’t have a body at all, only the URL), but the type of response that I want to receive?
On w3c and mozilla it is somehow said differently about this ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-11-07
@mgramin

So there is a special Accept header, in which the client specifies which formats it supports.
Ie:
- Accept: application/json
- Accept: text/xml
- Accept: text/plain, text/xml
This is the most correct option in terms of HTTP, but for convenience I would use the 2nd option (passing to URL).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question