actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
https://actix.rs
Apache License 2.0
21.75k stars 1.68k forks source link

Json extractor panics when used with HttpRequest with AppState #468

Closed chefsalim closed 6 years ago

chefsalim commented 6 years ago

This may be another issue with not using the framework correctly, however I haven't been able to get to the bottom of what is happening.

I have a handler with an extractor tuple of Json struct and a HttpRequest with appstate - eg,

fn my_handler((body, req): (Json<MyStruct>, HttpRequest<MyState>)) -> HttpResponse {
   ... stuff ..
}

The handler is registered like so:

.resource("/my/path", |r| { r.method(http::method::POST).with(my_handler) })

When this API is called, I get a panic and 400 return -

builder-api.default(O):  WARN 2018-08-14T21:38:21Z: actix_web::pipeline: Error occured during request handling: Content type error
builder-api.default(O): DEBUG 2018-08-14T21:38:21Z: actix_web::pipeline: ContentType
builder-api.default(O): 
builder-api.default(O): stack backtrace:
builder-api.default(O):    0:     0x556aa05099b5 - backtrace::backtrace::libunwind::trace::h951e0e0500314f91
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.9/src/backtrace/libunwind.rs:53
builder-api.default(O):                          - backtrace::backtrace::trace::h1ee62af1a87157f0
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.9/src/backtrace/mod.rs:42
builder-api.default(O):    1:     0x556aa050453e - backtrace::capture::Backtrace::new_unresolved::h0a72c4a0521b13d2
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.9/src/capture.rs:88
builder-api.default(O):    2:     0x556aa0502d99 - failure::backtrace::internal::InternalBacktrace::new::he87c8187bc7cd581
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/failure-0.1.2/src/backtrace/internal.rs:44
builder-api.default(O):    3:     0x556aa050259c - failure::backtrace::Backtrace::new::he3f5c7449d111eb3
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/failure-0.1.2/src/backtrace/mod.rs:111
builder-api.default(O):    4:     0x556a9f8e05ed - <actix_web::error::Error as core::convert::From<T>>::from::h25f1ff4c4eee4152
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/actix-web-0.7.3/src/error.rs:171
builder-api.default(O):    5:     0x556a9f8e0449 - <T as core::convert::Into<U>>::into::h8d6c9583c16263aa
builder-api.default(O):                         at /checkout/src/libcore/convert.rs:396
builder-api.default(O):    6:     0x556a9f6351ad - <actix_web::json::JsonConfig<S> as core::default::Default>::default::{{closure}}::h77df269ef7c76157
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/actix-web-0.7.3/src/json.rs:210
builder-api.default(O):    7:     0x556a9f635f57 - <actix_web::json::Json<T> as actix_web::handler::FromRequest<S>>::from_request::{{closure}}::h5e7d32adbe55465a
builder-api.default(O):                         at /tmp/cargo_cache/registry/src/github.com-1ecc6299db9ec823/actix-web-0.7.3/src/json.rs:148
builder-api.default(O):    8:     0x556a9f53c9a1 - <core::result::Result<T, E>>::map_err::h466fff4ab3efa1a4
builder-api.default(O):                         at /checkout/src/libcore/result.rs:500
builder-api.default(O):    9:     0x556a9f5b6032 - <futures::future::map_err::MapErr<A, F> as futures::future::Future>::poll::h56654fbce0542887

The API is getting called with the expected JSON, so it's not clear what is happening here, and whether this is a framework issue or pilot error.

chefsalim commented 6 years ago

Ah, this looks like it may be because the content type is not set correctly on the request - will verify and close out if so

chefsalim commented 6 years ago

OK confirmed it was the wrong content type :)

kevlarr commented 5 years ago

@chefsalim Thanks for posting this - I had not seen that one can use (body, req): (Json<..>, HttpRequest<..>) as the parameter signature, and that solved exactly what I needed!