JSON
POST request
Web API
Error handling
API integration

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/json header,
  • a body that is actually serialized JSON.

Example with JavaScript fetch:

javascript
1async function createUser() {
2  const payload = { name: "Lina", active: true };
3
4  const response = await fetch("https://example.com/api/users", {
5    method: "POST",
6    headers: {
7      "Content-Type": "application/json",
8      "Accept": "application/json"
9    },
10    body: JSON.stringify(payload)
11  });
12
13  if (!response.ok) {
14    throw new Error(`HTTP ${response.status}`);
15  }
16
17  return response.json();
18}

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.

csharp
1public class CreateUserRequest
2{
3    public string Name { get; set; } = string.Empty;
4    public bool Active { get; set; }
5}
6
7[ApiController]
8[Route("api/users")]
9public class UsersController : ControllerBase
10{
11    [HttpPost]
12    public IActionResult Create([FromBody] CreateUserRequest request)
13    {
14        return Ok(new { request.Name, request.Active });
15    }
16}

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:

  1. log the serialized JSON string,
  2. inspect network request headers,
  3. inspect full status code and response body,
  4. compare the payload shape with the server contract.

In JavaScript:

javascript
const body = JSON.stringify({ name: "Lina", active: true });
console.log(body);

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.

bash
1curl -X POST "https://example.com/api/users" \
2  -H "Content-Type: application/json" \
3  -H "Accept: application/json" \
4  -d '{"name":"Lina","active":true}'

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:

python
1import requests
2
3payload = {"name": "Lina", "active": True}
4response = requests.post("https://example.com/api/users", json=payload, timeout=10)
5print(response.status_code)
6print(response.text)

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 POST errors 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 curl or a second client to separate client bugs from server bugs.

Course illustration
Course illustration

All Rights Reserved.