[Actix] How to reveal the JSON string when it fails to be extracted as Json?

⚓ Rust    📅 2025-10-15    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 48 days ago. The information described in this article may have changed.

Actix extractors make it simple to get the target data types to directly handle in handlers, like

#[post("/records")]
async fn post_records(
    service_data: Data<X>,
    changes: Json<Changes>,
) -> (String, StatusCode);

But when client posts wrong Json format, by default, all I got was an unable to parse error. I want to get the JSON for debugging.

Seeing in its document, there is a JsonConfig I can play a little. But following code actually could not compile since the handler is not async.

                .app_data(JsonConfig::default().error_handler(|err, req| {
                    let bytes = Bytes::extract(req).await.unwrap();
                    let str = String::from_utf8(bytes.to_vec()).unwrap();
                    log::warn!("{:?}", str);
                    err.into()
                }))

So what is the correct way? If I do not want to just get the raw body in handler and deserialize by myself?

1 post - 1 participant

Read full topic

🏷️ Rust_feed