react native

React Native: Make a POST Request Using Fetch

What is a POST Request?

The POST request is part of the Http methods which is used as a request protocol between a client and a server.

A POST method typically sends data to the server. The type of the body of the request is indicated by the Content-Type header.

In creating mobile applications in React Native, there are times where you’ll need to submit a form or other resource to the server. This tutorial shows you how to do that using the Fetch API

Working With Fetch API

The Fetch API provides some type of interface for fetching resources from a network.

The fetch() method takes one mandatory argument, the path to the resource you want to fetch. It returns a Promise that resolves to the Response to that request, whether it is successful or not. You can also optionally pass in an init options object as the second argument.

Let’s see an example …

 fetch('https://api.github.com/users/defunkt', {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },

        body: JSON.stringify({
          name: 'Lawson Luke',
          email: 'email@yahoo.com',
          password: '123',
        }),
      })
        .then((response) => response.json())
        .then((responseJson) => {
          //Showing response message coming from server 
          console.warn(responseJson);
        })
        .catch((error) => {
        //display error message
         console.warn(error);
        });

Like we explained earlier, the fetch() takes a mandatory url, which is usually the endpoint through which you want to make your request

The body takes in your parameters. The name, email and password are parameterised destination from your server which must also receive a corresponding input.

The response object returns back a response from the server and here you can proceed to the next logic of your application

The catch() handles any error that results from making the request. These errors could be shown to the users or they could handled nicely as you deem fit.

Author

Recent Posts

Apple is developing a doorbell camera equipped with Face ID technology.

Apple is reportedly developing a new smart doorbell camera with Face ID technology to unlock…

3 hours ago

Google Launches Its Own ‘Reasoning’ AI Model to Compete with OpenAI

This month has been packed for Google as it ramps up efforts to outshine OpenAI…

2 days ago

You can now use your phone line to call ChatGPT when cellular data is unavailable.

OpenAI has been rolling out a series of exciting updates and features for ChatGPT, and…

4 days ago

Phishers use fake Google Calendar invites to target victims

A financially motivated phishing campaign has targeted around 300 organizations, with over 4,000 spoofed emails…

4 days ago

Hackers Exploiting Microsoft Teams to Remotely Access Users’ Systems

Hackers are exploiting Microsoft Teams to deceive users into installing remote access tools, granting attackers…

5 days ago

Ethical Hacking Essentials

Data plays an essential role in our lives.  We each consume and produce huge amounts…

7 days ago