Getting 400 for post request that receives JSON #2599
-
Given the code: async fn post<'de, D: Deserialize<'de>, S: Serialize, F: 'static + Copy + FnOnce(D) -> S>(this: Arc<JsonHandler<'de, D, S, F>>, Json(request): actix_web::web::Json<D>) -> impl Responder {
dbg!("enter");
let client = reqwest::Client::new();
let x = &(this.f)(request);
let result = client.post(this.to).json(x).send().await.map(|_| ()).map_err(|x| anyhow!(x));
match result {
Ok(_) => {
HttpResponse::NoContent()
}
Err(e) => {
eprintln!("ERROR!!!: {:?}", e);
HttpResponse::NotModified()
}
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dbg!("start");
HttpServer::new(|| {
App::new()
.service(
web::resource("/foobar")
.route(
web::post()
.guard(guard::Header("content-type", "application/json"))
.to(|a| h::<'static, TodoistPayload, DiscordWebhookPayload, _>( Arc::new(JsonHandler::new(
"https://discord.com/api/webhooks/xxxxxr",
|a: TodoistPayload| {
DiscordWebhookPayload {
content: "Yeah!".to_string(),
username: None,
avatar_url: None,
tts: false,
embeds: Default::default(),
components: Default::default()
}
}
)), a))
)
)
})
.bind("127.0.0.1:8080")?
.run()
.await;
dbg!("stop");
Ok(())
} When I try POST by I thought that was caused by serialization error: it does not have any required properties. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can take a look at
|
Beta Was this translation helpful? Give feedback.
You can take a look at
error::JsonPayloadError
for the several reasons whyJson
extractor could fail.JsonConfig
can be used to customize error handling. Alternatively, You can handle errors manually in the handler by usingResult<Json<D>, Error>
as extractor instead ofJson<D>
.