iron / body-parser Goto Github PK
View Code? Open in Web Editor NEWJSON body parsing for iron
License: MIT License
JSON body parsing for iron
License: MIT License
Would the maintainers be open to using Serde for deserializing JSON instead of rustc-serialize? If so, I'd be happy to submit a PR. Background: https://www.reddit.com/r/rust/comments/3we7fr/how_do_you_deal_with_upstream_crates_introducing/
Compiling fails using rust nightly, the allowed syntax for extern crate
seems to be different now:
[...]
Compiling bodyparser v0.0.4 (file:///home/niclas/Hacks/rust/body-parser)
src/lib.rs:9:14: 9:31 error: expected ident, found `"rustc-serialize"`
src/lib.rs:9 extern crate "rustc-serialize" as rustc_serialize;
^~~~~~~~~~~~~~~~~
Could not compile `bodyparser`.
See staticfile/router for examples.
I just copied the code from the readme, and it looks like the example is outdated. This is the error I get.
src/server.rs:38:15: 38:49 error: type `&mut iron::request::Request` does not implement any method in scope named `get`
src/server.rs:38 match req.get::<BodyParser<PleaseCompile>>() {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Also, the link to the docs is dead...
I was thinking it would be great if body parser would also fit the json in a struct while at it.
Problem is i'm unsure of
req.get::<StructParser<MyStruct>>()
https://gist.github.com/whodidthis/744560ae4c6eb4540973
Maybe there's another way?
Iron 0.6 has been released.
Are you already working on migration or I should fork and provide the migration?
Compiling bodyparser v0.0.5
/home/nathan/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.5/src/lib.rs:9:14: 9:31 error: expected ident, found `"rustc-serialize"`
/home/nathan/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.5/src/lib.rs:9 extern crate "rustc-serialize" as rustc_serialize;
^~~~~~~~~~~~~~~~~
rustc --version
rustc 1.0.0-nightly (abf0548b5 2015-04-15) (built 2015-04-16)
Currently body parser works by returning a Result<Option>
:
let struct_body = req.get::<bodyparser::Struct<MyStructure>>();
match struct_body {
Ok(Some(struct_body)) => println!("Parsed body:\n{:?}", struct_body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err)
}
I believe that adding an Option to the return type forces the consumer to have an extra level of matching for no gained benefit (in my personal opinion).
If I am trying to parse a body and there is no body present, I would argue that it should be an Err, instead of being Ok(None)
, after all, I was expected a value to be there that is missing. You can add MissingBody
as a potential error code.
I believe this would simplify the API and make it easier to consume.
If the maintainers of this repo agree with my proposal, I can go ahead and submit a PR.
Thanks.
v0.0.4 in github (compiles fine)
v0.0.5 on crates.io (does not compile)
Compiling bodyparser v0.0.1
Running `rustc /home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs --crate-name bodyparser --crate-type lib -g -C metadata=6d3d4ec3aa702856 -C extra-filename=-6d3d4ec3aa702856 --out-dir /home/xav/toysarust/target/deps --emit=dep-info,link -L dependency=/home/xav/toysarust/target/deps -L dependency=/home/xav/toysarust/target/deps --extern iron=/home/xav/toysarust/target/deps/libiron-cd5b38656a4d68ab.rlib -Awarnings -L native=/usr/lib/x86_64-linux-gnu -L native=/home/xav/toysarust/target/build/time-24f9c2dbbbc6f19b/out`
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:2:12: 2:31 warning: feature has been added to Rust, directive not necessary
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:2 #![feature(default_type_params)]
^~~~~~~~~~~~~~~~~~~
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:2:12: 2:31 warning: feature has been added to Rust, directive not necessary
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:2 #![feature(default_type_params)]
^~~~~~~~~~~~~~~~~~~
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:10:1: 10:21 error: can't find crate for `plugin`
/home/xav/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.1/src/lib.rs:10 extern crate plugin;
^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `bodyparser`.
I cloned this repository and tried to cargo build
it, but it fails:
src/lib.rs:30:37: 30:45 error: type `iron::request::Body<'_>` does not implement any method in scope named `by_ref`
src/lib.rs:30 match LimitReader::new(req.body.by_ref(), limit).read_to_end() {
^~~~~~~~
src/lib.rs:30:45: 30:45 help: methods from traits can only be called if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src/lib.rs:30:45: 30:45 help: candidate #1: use `std::io::ReadExt`
error: aborting due to previous error
Could not compile `bodyparser`.
Version:
$ rustc -V
rustc 1.0.0-nightly (3b3bb0e68 2015-03-04) (built 2015-03-05)
$ cargo -V
cargo 0.0.1-pre-nightly (dd7c7bd 2015-03-04) (built 2015-03-04)
I tried to fix it by adding use std::io::ReadExt
, but it is not the only error...
(I hope I didn't do anything wrong...)
Can you please show example for iron/router (not Chain)? When I use Router, I receive "no body".
Advertised documentation is missing
Serde 0.9 is currently usable on Rust beta (1.15) without any external codegen or feature gates, and 1.15 releases on Feb 2.
I tried to use the example as a reference to get my own code working, but it fails with:
error[E0277]: the trait bound `bodyparser::Raw: plugin::Plugin<iron::Request<'_, '_>>` is not satisfied
--> src/main.rs:27:24
|
27 | let body = req.get::<bodyparser::Raw>();
| ^^^ the trait `plugin::Plugin<iron::Request<'_, '_>>` is not implemented for `bodyparser::Raw`
|
= help: the following implementations were found:
<bodyparser::Raw as plugin::Plugin<iron::request::Request<'a, 'b>>>
The code in question:
let (tx, rx) = mpsc::channel();
let sender = Mutex::new(tx);
let chain = Chain::new(move |req: &mut Request| -> IronResult<Response> {
println!("received request");
let body = req.get::<bodyparser::Raw>();
match body {
Ok(Some(body)) => println!("Read body:\n{}", body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err),
}
let tx = sender.lock().unwrap().clone(); //Or whatever
tx.send("request");
return Ok(Response::with((status::Ok, "Hello World")));
});
chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH));
thread::spawn(move || {
for received in rx {
println!("{}", received)
}
});
match Iron::new(chain).http(env::var("BIND").unwrap()) {
Ok(_) => println!("fine"),
Err(error) => panic!("failed to bind HTTP listener: {:?}", error),
}
Please consider to add CHANGELOG.md to note breaking changes.
/home/suzart/.cargo/registry/src/github.com-1ecc6299db9ec823/bodyparser-0.0.5/src/lib.rs:116:1: 118:2 error: the trait core::marker::Reflect
is not implemented for the type T
[E0277]
Just found while compiling.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.