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

Paulo F. Oliveira paulo.ferraz.oliveira at gmail.com
Mon Sep 15 23:24:20 CEST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140915/26d4e023/attachment-0001.html>


More information about the Extend mailing list