Error sending json in POST to web API service
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a JSON POST request fails, the problem is usually not "JSON" in the abstract. It is almost always one of a few concrete issues: wrong headers, invalid serialization, incorrect request shape, or a server endpoint that expects something different from what the client sends. The fastest way to debug the failure is to inspect the exact payload, headers, and server response together.
The Minimum Correct JSON POST
A valid JSON POST needs three things:
- the correct URL,
- a
Content-Type: application/jsonheader, - a body that is actually serialized JSON.
Example with JavaScript fetch:
The common mistake is forgetting JSON.stringify, which sends [object Object] or an invalid payload shape.
A Reliable Server-Side Example
On the server, the endpoint must actually expect JSON.
If the server expects form data, XML, or a different JSON shape, the request will fail even if the client-side JSON is valid.
Debug The Actual Payload, Not The Assumption
Before changing code randomly, capture the request as sent.
Good debugging steps:
- log the serialized JSON string,
- inspect network request headers,
- inspect full status code and response body,
- compare the payload shape with the server contract.
In JavaScript:
That simple check catches many serialization bugs immediately.
Use curl To Isolate Client Versus Server Issues
If you suspect the application client is the problem, reproduce the request outside the app.
If curl works but your application does not, the issue is in client code. If both fail, inspect server contract and logs.
Common Causes Of JSON POST Errors
Wrong header
If Content-Type is missing or incorrect, the server may not bind the body as JSON.
Invalid body shape
A valid JSON document can still be wrong if the server expects different field names or nesting.
Server-side model binding mismatch
Strongly typed Web API endpoints often fail if required fields are missing or typed incorrectly.
Authentication or CSRF issues
Sometimes the payload is fine, but the server rejects the request due authorization or anti-forgery policy.
Character encoding problems
This is less common today, but malformed encodings or manual string construction can still break payload parsing.
Safer Client Patterns
Avoid building JSON by string concatenation. Serialize real objects instead.
In Python:
Using json=payload is safer than hand-building JSON strings.
Common Pitfalls
- Forgetting to serialize the body before sending it.
- Omitting
Content-Type: application/json. - Sending valid JSON that does not match the server's expected schema.
- Debugging only the client and never reading the server response body.
- Concatenating JSON manually instead of using a serializer.
Summary
- JSON
POSTerrors usually come from headers, serialization, or schema mismatch. - Always inspect the exact request body and response body together.
- Use serializers, not string concatenation, to build JSON.
- Verify the server endpoint really expects JSON in the shape you are sending.
- Use
curlor a second client to separate client bugs from server bugs.

