[99s-extend] REST responses

Loïc Hoguin essen at ninenines.eu
Tue May 20 20:46:02 CEST 2014


Hi,

On 05/20/2014 06:27 PM, Paulo F. Oliveira wrote:
> Hello.
>
> First of all, thanks for the great work you've done with cowboy. I've
> been using it with a fait amount of success and I'm a fairly new Erlang
> developer. I'm mainly interested in the REST "interface" of the
> application and its way of doing RESTful things, and I like the way you
> did it (what with all the content_types_provided, service_available,
> etc. functions). I've tested the way the system reacted to the different
> Accept, Content-Type, etc. headers and always got very well-opinionated
> responses (406, 415, ...).
>
> A couple of questions remain though (I'm sorry if they've been asked
> already but I've searched the web for answers and read the available
> docs and couldn't find them):
>
> 1. is it expected that, if I use cowboy_req:reply/2 in a GET handler
> (coming from content_types_provided), the onresponse/4 hook be called
> twice? I guess one is due to the reply and the other one due to the
> workflow of the request, but is there a way to prevent the second execution?

If you reply from a callback you must call {halt, Req, State} to stop 
processing.

> 2. if I want to JSON-parse ALL my requests should I a) use the
> onrequest/1 hook or b) do this on a per-request basis? Because I'd like
> to reply with a 400 ASAP but keep going if the JSON validates (I'm going
> to use JSON-schema for validating input); and, if possible, have the
> JSON-parsed body stored somewhere for future manipulation.

It makes little sense to do it before the accept callback you define. 
Not only because you will duplicate content-type checks and whatnot, but 
also because you don't actually win anything from doing this. If you are 
using JSON, then JSON processing will take infinitely more resources 
than the REST code.

> 3. I haven't seen examples that made use of the State (from the function
> returns). When should I use this instead of the Request metadata? I'd
> like to be able to set a generic error state for a request (either in
> meta ou State) and that have a "standard" error response be created at a
> later time (in a unique function, for example - e.g. onresponse/4).

State is for the functions within the current module. Look at 
cowboy_static for an example.

> 4. is there anything like a catch-all exception handler? I'd like to
> catch exceptions that occur anywhere so I could log them and analyze
> them at a later moment.

You can add your own error_logger handler, or use something like lager. 
All errors end up sending a message to error_logger.

> I'm probably abusing the onresponse/onrequest hooks already, so your
> answers should help me clarify this.

Sounds like it!

> Thanks.
>
> - Paulo
>
>
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> https://lists.ninenines.eu/listinfo/extend
>

-- 
Loïc Hoguin
http://ninenines.eu



More information about the Extend mailing list