views:

117

answers:

2

If a clients sends data in an unsupported media type to a HTTP server, the server answers with status "415 unsupported media type". But how to tell the client what media types are supported? Is there a standard or at least a recommended way to do so? Or would it just be written to the response body as text?

A: 

I believe you can do this with the OPTIONS Http verb.

Also the status code of 300 Multiple Choices could be used if your scenario fits a certain use case. If they send a request with an Accept header of application/xml and you only support text/plain and that representation lives at a distinct URL then you can respond with a 300 and in the Location header the URL of that representation. I realize this might not exactly fit your question, but it's another possible option.

And from the HTTP Spec:

10.4.7 406 Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

Unless it was a HEAD request, the response SHOULD include an entity containing a list of available entity characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. Depending upon the format and the capabilities of the user agent, selection of the most appropriate choice MAY be performed automatically. However, this specification does not define any standard for such automatic selection.

      Note: HTTP/1.1 servers are allowed to return responses which are
      not acceptable according to the accept headers sent in the
      request. In some cases, this may even be preferable to sending a
      406 response. User agents are encouraged to inspect the headers of
      an incoming response to determine if it is acceptable.
Gandalf
That'd work, except there's no spec for the content of the response there. It might be telling you, but how would you know?
Donal Fellows
But which header should be used to say what media types are supported? This header could then be used in the `415` response directly. Usually `OPTIONS` is only used to find out what methods are supported.
deamon
@Gandalf: `406` is irrelevant, as that's related to a type mismatch for the *response*. `415` is what you get when the server can't handle the type of the data in the *request* body. (I've just been dealing with this in the context of a RESTful webservice I'm developing, so I'm *sure* that's the right interpretation.) The problem is that the server can't handle the message and the client is already sending it; error is the only possibility (and there's no way to give a proper machine-readable way of saying what would have worked).
Donal Fellows
+1  A: 

There is no specification at all for what to do in this case, so expect implementations to be all over the place. (What would be sensible would be if the server's response included something like an Accept: header since that has pretty much the right semantics, if currently in the wrong direction.)

Donal Fellows
`Accept` headers from server to client would be what I'm looking for. I'm looking forward to HTTP 1.2 ;-)
deamon