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

后端 未结 4 452
猫巷女王i
猫巷女王i 2020-12-08 07:03

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 que

相关标签:
4条回答
  • 2020-12-08 07:18

    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 use a content-type request header.

    0 讨论(0)
  • 2020-12-08 07:18

    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.

    0 讨论(0)
  • 2020-12-08 07:18

    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.

    0 讨论(0)
  • 2020-12-08 07:21

    The Accept 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.

    HTTP header fields provide required information about the request or response, or about the object sent in the message body. There are four types of HTTP message headers:

    • General-header: These header fields have general applicability for both request and response messages.
    • Client Request-header: These header fields have applicability only for request messages.
    • Server Response-header: These header fields have applicability only for response messages.
    • Entity-header: These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request. Source

    https://www.w3.org/Protocols/HTTP/HTRQ_Headers.html
    https://www.w3.org/Protocols/HTTP/Object_Headers.html

    0 讨论(0)
提交回复
热议问题