You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It'd be really nice to be able to get the raw body of a request with something like request.raw_body(). This would allow request formats other than JSON and form encoded; it'd also allow the use of parsers other than rustc_serializable for JSON decoding (I need to use Serde, for instance).
The text was updated successfully, but these errors were encountered:
I've written a plugin to do this. It's probably a bit verbose, but it gets the job done. Posted here for anyone else who may need this functionality:
//! Provides a plugin for Nickel's Request which allows the raw contents//! of a body to be made available via the req.raw_body() method.externcrate plugin;externcrate typemap;use std::io;use std::io::Read;use nickel::Request;useself::typemap::Key;useself::plugin::{Plugin,Pluggable};structRawBodyPlugin;pubtraitRawBody{fnraw_body(&mutself) -> &str;}implKeyforRawBodyPlugin{typeValue = String;}impl<'mw,'conn,D>Plugin<Request<'mw,'conn,D>>forRawBodyPlugin{typeError = io::Error;fneval(req:&mutRequest<D>) -> Result<String, io::Error>{letmut buffer = String::new();
try!(req.origin.read_to_string(&mut buffer));Ok(buffer)}}impl<'mw,'conn,D>RawBodyforRequest<'mw,'conn,D>{fnraw_body(&mutself) -> &str{matchself.get_ref::<RawBodyPlugin>().ok(){Some(x) => x,None => "",}}}
To use:
use nickel::Nickel;mod nickel_rawbody;useself::nickel_rawbody::*;fnmain(){letmut server = Nickel::new();let prometheus = Prometheus::new().unwrap();
server.post("/",middleware!{ |request, response|
request.raw_body()});
server.listen("0.0.0.0:9999");}
I am thinking of pulling this into body_parser.rs, although is should probably be StrBody, since it returns the body as a unicode string. RawBody should probably return &[u8].
It'd be really nice to be able to get the raw body of a request with something like
request.raw_body()
. This would allow request formats other than JSON and form encoded; it'd also allow the use of parsers other than rustc_serializable for JSON decoding (I need to use Serde, for instance).The text was updated successfully, but these errors were encountered: