[99s-extend] Using cowboy_req:body more than once per request

Loïc Hoguin essen at ninenines.eu
Tue Sep 16 00:22:27 CEST 2014


It seems a bit weird to me to read the body and validate it before 
validating the request itself.

I would explicitly put these checks in the handler directly. This of 
course means that there is no need to read it twice anymore.

On 09/15/2014 11:24 PM, Paulo F. Oliveira wrote:
> Hi.
>
> I recently implemented a checksum header (X-Checksum) that allows
> validating the content of a request's body by hash comparison (just to
> give you some context). I'm using the onrequest hook to affect all
> requests (and be able to reply appropriately for non-conformance to the
> hash function result) but can't figure out how to not read the request
> body twice, i.e. I read it in the onrequest hook but later on need to
> read it again in the route handler, but I can't (from the manual, for
> cowboy_req:body: "This function can only be called once. Cowboy will not
> cache the result of this call."). At the moment, and because the API
> consumers were in a hurry, the solution I found (I understand it might
> be an ugly hack), was to read the body, store it in the Req's meta
> (property body, for example) and then access that property later on,
> instead of using cowboy_req:body. I'm not quite happy with this solution
> and was wondering if there is anything more elegant that I can implement.
>
> Thanks.
>
> Cheers.
>
> - Paulo F. Oliveira
>
>
> _______________________________________________
> 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