Search code examples
rustrust-rocket

Rocket cannot parse JSON


I'm having an error when trying to parse JSON with rocket. This is my code:

extern crate serde;
use rocket::serde::{json::Json, Deserialize};
use serde::Serialize;
#[macro_use]
extern crate rocket;
mod eth;
mod rocket_test;

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/task", routes![body])
}
#[derive(Serialize, Deserialize)]
struct Message2 {
    id: usize,
}

#[post("/", format = "json", data = "<message>")]
async fn body(message: Json<Message2>) -> &'static str {
    "Fine"
}

And this is my toml file:

[dependencies]
serde = {version = "1.0", features = ["derive"]}
serde_derive = "1.0"
serde_json = "1.0"
tester = "0.9.0"
tokio = {version = "1", features = ["full"]}

[dependencies.rocket]
features = ["json"]
version = "0.5.0-rc.1"

I'm getting this error

thread 'rocket-worker-thread' panicked at 'assertion failed: `(left == right)`
  left: `0x28f12470657`,
 right: `0x28f12470640`', C:\Users\gadum\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.16.0\src\io\util\take.rs:93:9
note: run with `RUST_BACKTRACE=1` environment variable 
to display a backtrace
   >> Handler body panicked.

It's just the same as in the example of JSON parsing. But I have no idea what is going wrong. I'm sending requests with VSCode Rest client just like this

POST http://127.0.0.1:8000/task
content-type: application/json

{
    "id": 10
}

Solution

  • It looks like version 1.16.0 of tokio has an error: https://github.com/tokio-rs/tokio/issues/4435

    Do a cargo update and it should update your dependency to 1.16.1.