Search code examples
resthttp-headersrfc

Is Accept header needed for a POST method which doesn't return any content to client?


I have an endpoint which supports POST method with content-type as JSON (only). But the POST request doesn't return any content in its response body other than status codes. In this scenario, what is the correct behavior?

  1. Client sends POST with Accept header as application/json
  2. Client sends POST with Accept header as application/xml

Should the server return error in case 2?


Solution

  • RFC 7231 describes the semantics of the Accept header

    A request without any Accept header field implies that the user agent will accept any media type in response.

    If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.

    The Accept header provided by the client should probably reflect the context of the request as seen by the client; for instance, a web browser might reasonably use a different Accept header for <img> than for <script>, in each case encouraging the server to provide useful representations.

    In the case of a POST, what you are trying to negotiate is the representation of "the status of, or results obtained from, the action", rather than a representation of resource itself.

    If the representation of the response is zero bytes long when the media-type is application/json, then I would expect the response to also be zero bytes long when the media-type is application/xml. So it isn't obvious to me to accept one but not the other.