I am using ASP.NET Web Api and one of the methods is exposed as a POST operation. It works fine, but it returns a HTTP Result of 204 instead of 201 as expected. Here is the method definition:
Response code: By default, the Web API framework sets the response status code to 200 (OK). But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201
(Created).
You would need to return a HttpResponseMessage with Status=Created as you suggested.
If you return void from a normal WebAPI method then the framework returns 204 by default, IIRC -- it means no content. If you just return Task then that's like void but just asychronously, so you'll still get 204.
K I changed the code to the 2nd example in my post which works ok. However, this should be fixed in a future update. Obviously any POST that doesn't fault should return a 201, asynchronous or not.
aemami99
Member
17 Points
13 Posts
ASP.NET Web Api HttpPost Task returning a 204 instead of 201
Nov 14, 2012 10:26 PM|LINK
I am using ASP.NET Web Api and one of the methods is exposed as a POST operation. It works fine, but it returns a HTTP Result of 204 instead of 201 as expected. Here is the method definition:
[HttpPost][ActionName("Save")] public Task SaveGameState(Guid instanceId, [FromBody] ComparisonGameState state) { return gameInstancesClient.SaveGameState(instanceId, state); }Is the fact that it is returning a Task confusing Web Api?
I could do the following instead, but it seems like overkill:
[HttpPost][ActionName("Save")] public async Task<HttpResponseMessage> SaveGameState(Guid instanceId, [FromBody] ComparisonGameState state) { await gameInstancesClient.SaveGameState(instanceId, state); return new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.Created }; }Thank you!
ozkary
Contributor
2034 Points
303 Posts
Re: ASP.NET Web Api HttpPost Task returning a 204 instead of 201
Nov 15, 2012 03:05 PM|LINK
Response 200 should be the default.
Response code: By default, the Web API framework sets the response status code to 200 (OK). But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201 (Created).
You would need to return a HttpResponseMessage with Status=Created as you suggested.
og-bit.com
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: ASP.NET Web Api HttpPost Task returning a 204 instead of 201
Nov 15, 2012 03:11 PM|LINK
If you return void from a normal WebAPI method then the framework returns 204 by default, IIRC -- it means no content. If you just return Task then that's like void but just asychronously, so you'll still get 204.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
aemami99
Member
17 Points
13 Posts
Re: ASP.NET Web Api HttpPost Task returning a 204 instead of 201
Nov 15, 2012 06:30 PM|LINK
K I changed the code to the 2nd example in my post which works ok. However, this should be fixed in a future update. Obviously any POST that doesn't fault should return a 201, asynchronous or not.