Consume API with React

Asked

Viewed 1,708 times

1

someone can help me how I can get this API

## Cadastro
```sh
$ curl --request POST \
 --url https://dev.people.com.ai/mobile/api/v2/register \
 --header 'content-type: application/json' \
 --data '{
"email":"[email protected]",
"name":"Lennon Coelho",
"password":"Senha@12346"
}'

## Login
```sh
$ curl --request POST \
 --url https://dev.people.com.ai/mobile/api/v2/login \
 --header 'content-type: application/json' \
 --data '{
"email":"[email protected]",
"password":"Senha@12346"
}'

1 answer

3


You must use the fetch method by entering the API address, identify the method (POST, PUT,GET, DELETE), insert the required header and send the data in the body of the request.

Register

fetch('https://dev.people.com.ai/mobile/api/v2/register/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: '[email protected]',
    name: 'Lennon Coelho',
    password: 'Senha@12346',
  })
}).then((response) => {
    return response;
});

Login

fetch('https://dev.people.com.ai/mobile/api/v2/login/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: '[email protected]',
    password: 'Senha@12346',
  })
}).then((response) => {
    return response;
});

If this is your first time using fetch, in some cases you will need to install it and import it, follow the link.

Also you can install some extension like Axios or jQuery AJAX facilitating the call for REST methods.

  • More guy let me ask you if you can help me could give me an example by passing the name of the API to a components just how to pass

  • just insert the name into a variable and put this.variable instead of the API link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.