Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header parameters: "Accept" and "Content-type" in a REST context

I understand that the Accept parameter define a data type expected in a client response sent from the server, so it's used as a response header.

My question is regarding the Content-type, it's used by a client to define the body format of a request sent, I always used it as part of a client request, so I have a client request where I set the headers with Accept and Content-type. And recently, I came across a project where the Content-type is defined in the response headers (so sent by the server). So my question is: Content-type need to be set as part of the client request header or as part of the server response header or can it be set to both ?

like image 521
Emilien Brigand Avatar asked Mar 01 '16 12:03

Emilien Brigand


People also ask

What is accept and Content-Type in headers?

The Accept is Client Request-header field can be used to specify certain media types which are acceptable for the response. The Content-Type is entity-header field indicates the media type of the entity-body sent to the recipient.

What is Content-Type header in REST API?

The Content-Type field in the HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API.

What is header parameter in REST API?

The REST headers and parameters contain a wealth of information that can help you track down issues when you encounter them. HTTP Headers are an important part of the API request and response as they represent the meta-data associated with the API request and response.

How do I pass a header in REST API?

You can pass duplicate headers as well and there will not be any overwritten of values. For example, If we pass two values of header1 as value1 and value2 then it will be merged and will be passed as header1=value1 and header1=value2. It is the default behaviour.


4 Answers

Read the relevant RFCs. In this case 7231:

5.3.2. Accept

The "Accept" header field can be used by user agents to specify response media types that are acceptable.


3.1.1.5. Content-Type

The "Content-Type" header field indicates the media type of the associated representation

So: Accept indicates what kind of response from the server the client can accept. Content-type always is about the content of the current request or response.

So if your request has no payload, you don't have to use a content-type request header.

Servers may require you to provide a content-type in a request even if the request has no payload; the sever will return a 415 Unsupported Media Type response if you omit it.

like image 189
CodeCaster Avatar answered Nov 07 '22 06:11

CodeCaster


Accept header is used by HTTP clients to tell the server which type of content they expect/prefer as response. Content-type can be used both by clients and servers to identify the format of the data in their request (client) or response (server) and, therefore, help the other part interpret correctly the information.

like image 26
Alberto Avatar answered Nov 07 '22 07:11

Alberto


TL;DR

The entity header Content-Type is used to indicate the media type of the resource. In responses, a Content-Type header tells the client what the content type of the returned content actually is. In requests, such as POST or PUT, the client tells the server what type of data is actually sent.

Elaborated Answer

As you correctly note, the Accept header is used by HTTP clients to tell the server what response media types are acceptable. The server, on their turn, will then send back a response, which will include the Content-Type header telling the client what the media type is actually returned.

Now, the Content-Type header could be on request and responses as well. 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.

like image 35
Johnny Avatar answered Nov 07 '22 07:11

Johnny


I think this is explained in MSDN very clear.

Accept

The Accept request HTTP header advertises which content types, expressed as MIME types, the client is able to understand. Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Type response header. Browsers set adequate values for this header depending on the context where the request is done: when fetching a CSS stylesheet a different value is set for the request than when fetching an image, video or a script.

Content-Type

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending).

In responses, a Content-Type header tells the client what the content type of the returned content actually is. Browsers will do MIME sniffing in some cases and will not necessarily follow the value of this header; to prevent this behavior, the header X-Content-Type-Options can be set to nosniff.

In requests, (such as POST or PUT), the client tells the server what type of data is actually sent.

like image 44
Murat Can OĞUZHAN Avatar answered Nov 07 '22 06:11

Murat Can OĞUZHAN