Emulating Webmachine's {halt, StatusCode} in Cowboy
At 2600Hz, we recently converted our REST webserver from Mochiweb/Webmachine to Cowboy, with cowboy\http\rest giving us a comparable API to process our REST requests with. One feature that was missing, however, was an equivalent to Webmachine's {halt, StatusCode} return. While there has been chatter about adding this to cowboy\http\rest, we've got a function that emulates the behaviour pretty well (this is cleaned up a bit from our actual function, removing project-specific details).
-spec halt/4 :: (#http_req{}, integer(), iolist(), #state{}) -> {'halt', #http_req{}, #state{}}. halt(Req0, StatusCode, RespContent, State) -> {ok, Req1} = cowboy_http_req:set_resp_body(Content, Req0), {ok, Req2} = cowboy_http_req:reply(StatusCode, Req1), {halt, Req2, State}.
Obviously you can omit setting the response body if you don't plan to return one.