Fetch, POST JSON data
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The Fetch API in JavaScript is a powerful tool for making network requests, similar to XMLHttpRequest. It is more powerful and flexible, as it handles requests asynchronously using Promises, making it a better choice for modern web development. One of the common uses of the Fetch API is to send (POST) JSON data to a server. This is typically done when you need to submit data from a form or control a web application.
Understanding the Fetch API
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It not only allows you to request resources but also to send data to a server in various formats including JSON, which is a common format for encoding data in web applications.
How to POST JSON Data Using Fetch
To send JSON data using the Fetch API, you need to create an HTTP POST request, and include the JSON data in the body of the request. Here is a step-by-step guide on how to do it:
1. Create JSON data
First, you need to create the JSON object that you will send to the server. This is generally derived from form inputs or dynamic data within your application.
2. Use Fetch to send the request
Use the fetch() function to send a POST request. You need to specify the URL of the API endpoint, and also provide a configuration object which includes the method, headers, and the body.
Detailed Breakdown of Fetch Options
- URL: The first parameter of
fetch()is the URL to which the request is sent. - Method: The HTTP method, e.g.,
POST,GET,PUT,DELETE, etc. - Headers: Set of request headers.
Content-Type: application/jsonis necessary to let the server know that the request body format is JSON. - Body: Data to be sent to the server. It must be a string, so if you are sending JSON data, use
JSON.stringify()to convert the object into a JSON string.
Error Handling
Handling errors with Fetch correctly is crucial for building reliable applications:
In this example, it checks whether response.ok is true, which is a shorthand for status range 200-299, indicating that the HTTP status code was successful.
Summary Table
| Feature | Functionality |
| URL | Specifies the endpoint URL. |
| Method | HTTP method used (e.g., POST, GET). |
| Headers | Request headers (e.g., Content-Type: application/json). |
| Body | Data sent to server, as a JSON string. |
| Error Handling | Handling fetch and server errors properly. |
| Response Conversion | Converting the raw response into JSON or text format. |
Conclusion
Using the Fetch API to post JSON data is a robust method for modern web development, facilitating server communication in a promise-based manner. By understanding and properly utilising this API, developers can integrate sophisticated functionalities into their web applications efficiently.
Related reading
- Fetch request failing in Node.js Client network socket disconnected before secure TLS connection was established
- Field required a bean of type that could not be found. error spring restful API using mongodb
- Filtering fiddler to only capture requests for a certain domain
- Find the next TCP port in .NET
- Find the paths between two given nodes?
- Finding local IP addresses using Python's stdlib
- Flannel and docker don't start
- Flask API as real time kafka consumer

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.