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
Comments 0
No clarifications yet.
Sign in to add clarification.