LINUX.ORG.RU

HTTP, код респонза.


0

0

Ситуация: есть HTTP-сервер, рукодельный, т.н. "RESTfull aplication". Принимает данные от клиента в качестве тела POST и PUT запросов. Разумеется, данные шлются в определённом формате, и если клиент пришлёт фигню, на сервере она не распарсится.

Вопрос: как сообщать об этом клиенту? Варианты: 400 Bad Request - как-то мимо, это когда вообще сервер не смог распарсить сам реквест и заголовки. 500 - Internal Server Error - тоже мимо, клиент же фигню прислал, почему на сервере еррор? 403 - как-то тоже слабо.

Вот что-то более подходящее: отвечать 200 OK, но в теле ответа писать об ошибке. Или вот есть такой 415 Media Not Supported - оно? В rfc2616 как-то непонятно для меня описано назначение этого дела.

Советую глянуть книгу „RESTful Web Services“ от O’Reilly.

Цитата из книги:

415 (“Unsupported Media Type”)

The server sends this status code when the client sends a representation in a media type it doesn’t understand. The server might have been expecting XML and the client sent JSON.

If the client sends a document that’s got the right media type but the wrong format (such as an XML document written in the wrong vocabulary), a better response is the more generic 400 (“Bad Request”). That’s what I use throughout my bookmark service. If you really want to treat that case specially, you can send the WebDAV extended response code 422 (“Unprocessable Entity”).

WFrag ★★★★
()
Ответ на: комментарий от WFrag

До кучи:

400 (“Bad Request”)

This is the generic client-side error status, used when no other 4xx error code is ap-propriate. It’s commonly used when the client submits a representation along with a PUT or POST request, and the representation is in the right format, but it doesn’t make any sense.

Entity-body: May contain a document describing why the server thinks there’s a client-side error.

WFrag ★★★★
()
Ответ на: комментарий от WFrag

Спасибо.

http://tools.ietf.org/html/rfc2616#section-10.4.1

>The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.


Интересно, оно не противоречит цитате из книги? Речь идёт о синтаксисе HTTP-запроса, или о синтаксисе того, что этот запрос несёт, т.е. уровнем выше?

Score-49
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.