Post JSON to a Server

Sending JSON Data to a Server
To post JSON data to a server, include the JSON in the HTTP POST request body and set the Content-Type: application/json header. This header tells the server the type of data being sent. You can also use the Accept: application/json header to let the server know you expect a JSON response. For instance, when sending JSON to the ReqBin echo URL, both Accept and Content-Type headers are used for proper communication between the client and server.

What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based format used for exchanging data. It is language-independent and follows a simple set of rules to represent structured data. JSON supports:

  • Primitive types: strings, numbers, booleans, and null.
  • Structured types: objects and arrays.
    Because of its simplicity and compatibility, JSON is widely used in various programming languages, such as JavaScript, Python, Java, C++, and PHP.

Understanding HTTP POST
The HTTP POST method allows clients to send data to the server for creating or updating resources. This method is commonly used for submitting web forms, uploading files, and transferring JSON data.

MIME Type for JSON Data
The official media type for JSON is application/json, as defined by RFC4627. When sending JSON data via HTTP POST, PUT, or PATCH, always include the Content-Type: application/json header. If the server responds with JSON, it should also include the Content-Type: application/json header to ensure proper handling by the client.

Example of a JSON Content-Type Header
Here’s what a Content-Type header for JSON looks like:

Content-Type: application/json

Requesting JSON from a Server
If your client expects JSON data, include the Accept: application/json header in your request. Without this header, the server might return data in another format, such as XML. For APIs that support multiple formats, the Accept header ensures the response is formatted as JSON.

Example: Sending JSON Data via POST
Below is an example of sending JSON data to the ReqBin echo URL using the POST method:

POST /echo HTTP/1.1  
Host: reqbin.com  
Content-Type: application/json  
Accept: application/json  

{  
  "name": "Dharmender Singh",  
  "age": 30,  
  "city": "Bangalore"  
}

Click “Send” to execute the request and observe the server’s response.


JSON is a crucial format for modern web development, allowing seamless data exchange between servers and clients. By properly configuring HTTP headers like Content-Type and Accept, you can ensure smooth communication and accurate data handling.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *