Reqwest 0.11.1 POST body serialization: Body: From<T> is not satisfied

Hi everybody!

I'm having an issue with reqwest v0.11.1, the following won't compile:

pub async fn create_mailjob(&self, job: MailJob) -> Result<bool, reqwest::Error> {
        let response = reqwest::Client::new()
            .post(&self.url)
            .body(&job)
            .send()
            .await?;
         response.json::<bool>().await
    }

With MailJob being:

MailJob struct
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct MailJob {
    project_id: String,
    schedule: u16,
    day: DayOfWeek,
    email: String,
    report_data_type: ReportDataType,
}

#[derive(Serialize, Deserialize)]
pub enum ReportDataType {
    ReportA,
    ReportB,
    // etc... only name values
}

#[derive(Serialize, Deserialize)]
pub enum DayOfWeek {
    Sunday,
    Monday,
    // etc ...
}

I get the following error:error[E0277]: the trait bound Body: From<MailJob> is not satisfied

error[E0277]: the trait bound `Body: From<&MailJob>` is not satisfied
  --> src\mailjob.rs:55:54
   |
55 |         let response = Client::new().post(&self.url).body(&job).send().await?;
   |                                                      ^^^^ the trait `From<&MailJob>` is not implemented for `Body`
   |
   = help: the following implementations were found:
             <Body as From<&'static [u8]>>
             <Body as From<&'static str>>
             <Body as From<Response>>
             <Body as From<Vec<u8>>>
           and 2 others
   = note: required because of the requirements on the impl of `Into<Body>` for `&MailJob`

I checked with cargo tree that reqwest uses the same versions both for serde and serde_json that I'm using, and made sure the json and derive features are enabled for reqwest and serde respectively.

Since MailJob is a rather lightweight struct, I'm not sure what I'm doing wrong.

I'd appreciate some help from the community :slight_smile:

It's .json(&job), not .body(&job).

https://docs.rs/reqwest/0.11.1/reqwest/struct.RequestBuilder.html#method.json

1 Like

...and now I realized that .body(&job) should instead have been .json(&job).
In my defence, they're both 4-characters long :sweat_smile:

Hope this help anybody else having the same duh moment in the future. Thanks anyway!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.