Search code examples
resterlangcowboy

How to redirect with cowboy_rest


I am having trouble with how to redirect with cowboy_rest with GET request. Is there any way to set the <<"location">> in header to response to browser by cowboy_rest?

The redirect is made in the cowboy_rest with POST method is {{ok, URL_to_redirect}, Req, State}

But I cannot find a way to redirect with GET method even <<"location">> is set in the header of cowboy_req.

Thanks,


Solution

  • You can use cowboy_req:reply/4 and {halt, Req, State} to stop execution:

    {ok, NewReq} = cowboy_req:reply(
                     302,
                     [{<<"Location">>, <<"http://www.redirect-url.com">>}],
                     <<>>,
                     Req),
    {halt, NewReq, State}