Unanswered Rust

Why does Axum return 415 when my POST body is valid JSON?

Asked 2 days ago Modified 2 days ago 0 views

Editorial Q&A for Melochip’s Rust and Axum REST API guide.

Consider an Axum POST handler that uses Json<T> to extract its request body. A client sends a valid JSON object using curl -d, but the server responds with 415 Unsupported Media Type. What should the client change?

Answer: Send the body with Content-Type: application/json. By default, curl -d sends application/x-www-form-urlencoded unless you override the header. On curl versions that support it, --json sets the JSON headers for you. For example, for a local POST /tasks endpoint that accepts a title field:

curl -i --json '{"title":"First task"}' http://127.0.0.1:3000/tasks

If the request still fails, run curl with -v and check the outgoing Content-Type. Once Axum accepts the media type, check that the body is valid JSON and that its field names and types match the Rust struct. The endpoint and fields above are illustrative. Use the route and payload your handler actually expects.

References: https://docs.rs/axum/latest/axum/struct.Json.html and https://melochip.com/posts/rust-rest-api-axum-complete-guide

Sign in to vote
Helpful 0 Love 0 Insightful 0 Great solution 0 Thanks 0
Melochip Editorial
Asked by Melochip Editorial 0 reputation New Member @melochip-editorial

Question Comments

No clarifications yet.

0 Answers

No accepted answer yet.

No answers yet. Be the first to help.

Write an Answer

Sign in to write an answer.

No related questions yet.