Bolboa Bolboa - 1 month ago 11
Node.js Question

How to pass JSON object from API to server

I am using

React
and
node.js
, and I've used react's
fetch
to
POST
some Login credentials to my restAPI in order to receive a webtoken...

fetch('http://localhost:8080/api/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.username_login,
password: this.state.password_login
})
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson); //this is the object containing the token
})

.catch(function(error) {
console.log("request failed");
})
}


so
responseJson
is the the object containing my web token. The request was successful and now I've passed it to the client. Now, I am thinking of saving it to a
cookie
.

How can I send this to the server? Should I make a
POST
request? If so how do I do that once the JSON object is received? If there is a better way, I would like to know.

Answer

Cookies are automatically passed along any HTTP request made with fetch

Comments