1
votes

I'm building a REST API and trying to return 405 Method Not Allowed for a particular route when a HEAD request comes in.

The rules say "The response MUST include an Allow header containing a list of valid methods for the requested resource."

However, the rules also say "the server MUST NOT return a message-body in the response."

So I'm constructing my HttpResponseMessage with its Content property null, but the only way I can supply an "Allow" header is as a content header. If I try to supply it as a header on the HttpResponseMessage object instead of the HttpContent object, I get an error:

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

So is it possible to comply with the spec or is it one of those "hold your nose and ship" situations?

1
Please, show the exact code used for creating the response. BTW, the 2nd rule refers to a regular answer to the HEAD request. But, if the HEAD request is not allowed, you are not returning a regular response, but another kind of response (405). So the rule of "no content body" doesn't apply to the 405 response. It would apply only to a regular response to HEAD. The problem is somewhere else.JotaBe

1 Answers

0
votes

Try sending a ByteArrayContent with your header and a zero-length byte array.