Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native fetch not sending body content

Tags:

react-native

I am new to fetch in react native and followed this tutorial.
I am trying to send a json body to an private api server, I checked the server log and found out that the body content is empty.

Here is the code in react native

authenticateLogIn(){
    fetch('<URL>', {
          method: 'POST',
          header: {'Content-Type': 'application/json', 'Accept': 'application/json'},
          body: JSON.stringify({'username': '<username>', 'password':'<password>'})
        })
        .then((incoming) => incoming.json())
        .then((response) => {
          console.log(response.header);
          Alert.alert(JSON.stringify(response.body));
        })
        .done();
like image 744
XPLOT1ON Avatar asked Aug 13 '16 20:08

XPLOT1ON


2 Answers

Maybe because it shoueld be headers? (with the s)

like image 53
user1442397 Avatar answered Oct 23 '22 20:10

user1442397


It might be because your JSON body isn't being parsed correctly. bodyParser https://github.com/expressjs/body-parser is part of the middleware that parses request bodies and needs to be configured.

req.body kept coming back empty for me until I added app.use(bodyParser.json() into server.js file. Make sure you import body-parser.

like image 45
user10789654 Avatar answered Oct 23 '22 20:10

user10789654