简体   繁体   中英

GET call on REST API with Accept as text/csv and content-Type as application/json. In what format should the reponse be?

GET call on REST API with Accept as text/csv and content-Type as application/json. In what format should the reponse be?

Should the response be in JSON format or in CSV format?

As you correctly note, the Accept header is used by HTTP clients to tell the server what content types they'll accept. The server will then send back a response, which will include a Content-Type header telling the client what the content type of the returned content actually is.

However, as you may have noticed, HTTP requests can also contain Content-Type headers. Why? Well, think about POST or PUT requests. With those request types, the client is actually sending a bunch of data to the server as part of the request, and the Content-Type header tells the server what the data actually is (and thus determines how the server will parse it).

In particular, for a POST request resulting from an HTML form submission, the Content-Type of the request will (normally) be one of the standard form content types below, as specified by the enctype attribute on the tag:

application/x-www-form-urlencoded (default, older, simpler, slightly less overhead for small amounts of simple ASCII text, no file upload support) multipart/form-data (newer, adds support for file uploads, more efficient for large amounts of binary data or non-ASCII text)

source: https://webmasters.stackexchange.com/users/12578/ilmari-karonen

based on that, you should go for the parameter Accept!

In HTTP, the Accept header is used by the client to tell the server what content types they'll accept. The server will then send back the response and will set the Content-type header telling the client the type of the content actually returned.

You might have noticed that Content-type is also included in some HTTP requests. This is because some type of HTTP requests, like POST or PUT, can send data to the server. In this case the client tells the server the type of the content data, using the Content-type header.

Now to your question, a GET request should not have any Content-type header. I hope this is clear after my explanation above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM