Reqres and Fetch API

Today we are going to make a POST request using Reqres and Fetch API in JavaScript. We’ve previously seen how to make a GET request. But before we go straight to coding, let’s see what a POST request is.

What is a POST Request?

A POST request is one of the HTTP request methods used by web browsers and servers to send data to the server. When a client (typically a web browser) sends a POST request to a server, it includes data in the body of the request, which the server then processes. POST requests are often used for submitting data such as form inputs, file uploads, or any other data that needs to be processed by the server.

Here are some key points about POST requests:

  1. Data in the Body: Unlike GET requests, which include data in the URL, POST requests send data in the body of the request. This allows for larger amounts of data to be sent and doesn’t expose sensitive information in the URL.
  2. Security: POST requests are generally considered more secure for sending sensitive data, such as login credentials or payment information, because the data is not visible in the URL and is encrypted if using HTTPS.
  3. Idempotent: POST requests are not idempotent, meaning that multiple identical POST requests may have different effects on the server. For example, submitting a form twice may create two separate entries in a database.
  4. Usage: POST requests are commonly used for submitting HTML forms, uploading files, and performing actions that modify data on the server, such as creating, updating, or deleting resources in a RESTful API.
  5. Response: After processing the data sent in a POST request, the server typically responds with an appropriate HTTP status code to indicate the outcome of the request (e.g., 200 OK for success, 400 Bad Request for invalid input, etc.) and optionally returns data in the response body.

Next, we are going to create a form with an email and password inputs with Bootstrap for styling:

Here’s what it looks like:

Next, we are going to post data to our endpoint: https://reqres.in/api/login

This is the request we are making:

And this is the response we are expecting:

So, we will get the values from our input using querySelector as follows:

So here we are getting the form values and alerting it for testing purposes:

Reqres and Fetch API

Next, we will use Fetch API to consume our endpoint:

A few explanation is necessary here:

  1. We’re using e.preventDefault() because we don’t want the page to refresh after submission. This is just for testing purpose.
  2. The request body is converted to JSON format using JSON.stringify() because the Content-Type header is set to application/json.
  3. The response is checked for errors using response.ok. If the response status is not in the range 200-299, an error is thrown.
  4. You can handle the response data in the second .then() block or handle any errors in the .catch() block.

So to test our endpoint, we need to pass the expected values which are believed to be in our database and also so we can simulate for a response:

Here’s the full code:

Here’s the result:

Reqres and Fetch API

This is the token we’re getting on a successful login:

Reqres and Fetch API

Conclusion

This is how to make a post request using Fetch API in JavaScript. Let me know what you think in the comments.

By admin

Leave a Reply

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