Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a HTTP 405 status response allowed to have a body?

I am building a RESTful API. When the client uses an unsupported method such as POST on a resource that does not supports it, I am returning a 405 with the Allow header which lists the allowed methods:

Status Code: 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS
Connection: keep-alive
Date: Mon, 08 Apr 2013 00:19:26 GMT
Transfer-Encoding: chunked

Is it allowed to have a body (to provide an error message) for 405 responses?

From the w3c's site, it is not apparent whether a body is allowed for 405 responses.

like image 652
F21 Avatar asked Apr 08 '13 00:04

F21


People also ask

What is HTTP Status 405 Method not allowed?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method. The server must generate an Allow header field in a 405 status code response.

What is a code 405?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

How do I create a 405 error?

HTTP response status code 405 means Method Not Allowed . This status code states that HTTP method was received and recognized by the server, but the server has rejected that particular method for the requested resource. Try to access an existing resource without the proper permission.


1 Answers

The HTTP/1.1 protocol spec states in the "message length" section:

Any response message which "MUST NOT" include a message-body (the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.

A message body is optional for all 4xx-5xx codes as long as the HTTP method is not HEAD.

like image 198
Dmitry S. Avatar answered Sep 23 '22 05:09

Dmitry S.