Rashhh Rashhh - 2 months ago 20
Javascript Question

Unexpected end of JSON input error in Post request on api using access token in react-native

I am posting the details of user in api ,using the access token in header which i got in sign up but getting this error --> Unexpected end of JSON input. My code is

postNameToApi()
{

console.log("inside post api");
fetch('https://MyPostApi', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer'+'Qwjubq41KAWw9uI2NMj4TPQ9t24PxC'
},


body: JSON.stringify({
dob:'1992-04-18',
gender: 'femanino',
is_professional:true,
is_referee:false

})
}).then((response) => response.json())
.then((responseData) => {
console.log("inside responsejson");
console.log('response:',responseData);

//this.setState({response:responseData});
}).done();
}

Answer

This is because your response is not in json format. Space is missing between Bearer and your token, i think this will solve your issue.

'Authorization':'Bearer '+'Qwjubq41KAWw9uI2NMj4TPQ9t24PxC'

Try your api call with postman first.