Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot set 'Allow' in HTTP response header?

I've written a RESTful API using ASP.NET Web Api. Now I'm trying to make it returns the allowed verbs for a controller. I'm trying to do it with the following code:

[AcceptVerbs("OPTIONS")]
public HttpResponseMessage Options()
{
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Headers.Add("Access-Control-Allow-Origin", "*");
    response.Headers.Add("Access-Control-Allow-Methods", "POST");
    response.Headers.Add("Allow", "POST");

    return response;
}

But instead of getting a Allow Header on my response, I'm getting a 500 Internal Server Error. While debugging I receive the following error:

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

Is that possible to set that header?

like image 300
Glauco Vinicius Avatar asked Jan 11 '13 20:01

Glauco Vinicius


2 Answers

As the error message says, you must use content headers with HttpContent objects.

response.Content.Headers.Add("Allow", "POST");

Must admit this is kinda weird API...

like image 174
Esailija Avatar answered Oct 15 '22 20:10

Esailija


Allow is a content header.

        response.Content.Headers.Allow.Add("POST");
like image 33
Filip W Avatar answered Oct 15 '22 21:10

Filip W