0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Properly send 201 on PUT requests when resource didn't exist

Regardless of whether a location header has been set, as explained
in the HTTP RFC.
This commit is contained in:
Loïc Hoguin 2013-11-09 18:26:49 +01:00
parent a0205779fe
commit 5d27d4d175

View file

@ -801,8 +801,12 @@ process_content_type(Req, State=#state{method=Method, exists=Exists}, Fun) ->
error_terminate(Req, State, Class, Reason, Fun) error_terminate(Req, State, Class, Reason, Fun)
end. end.
%% If the resource is new and has been created at another location %% If PUT was used then the resource has been created at the current URL.
%% we send a 201. Otherwise we continue as normal. %% Otherwise, if a location header has been set then the resource has been
%% created at a new URL. If not, send a 200 or 204 as expected from a
%% POST or PATCH request.
maybe_created(Req, State=#state{method= <<"PUT">>}) ->
respond(Req, State, 201);
maybe_created(Req, State) -> maybe_created(Req, State) ->
case cowboy_req:has_resp_header(<<"location">>, Req) of case cowboy_req:has_resp_header(<<"location">>, Req) of
true -> respond(Req, State, 201); true -> respond(Req, State, 201);