I am trying to do a simple call to an API endpoint using the following code and keep getting the following error:
POST net::ERR_ABORTED 400 (Bad Request)
SyntaxError: Unexpected end of input
The request options
const requestOptions = {
method: 'POST',
mode: 'no-cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: email,
password: password
})
};
The fetch request
await fetch("https://dev.rapptrlabs.com/Tests/scripts/user-login.php", requestOptions)
.then(async response => await response.text())
.then(response => response === "" ? {} : JSON.parse(response) )
.catch(e => {
console.log(e)
})
I am not sure what I am doing wrong.
Edit: I was only provided with the following documentation:
a. When the form has been successfully submitted passing all validation errors, call the following API: http://dev.rapptrlabs.com/Tests/scripts/user-login.php i. This API endpoint needs to be called with a POST request. ii. It takes two body params: email and password. iii. For testing purposes, use the following email and password: test@rapptrlabs.com / Test123. iv. A successful response will look like the following:
{ "user_id": 16, "user_email": "test@rapptrlabs.com", "user_username": "testuser", "user_is_active": 1, "user_profile_image": "http://dev.rapptrlabs.com/Tests/images/taylor_avatar.png", "user_last_active_epoch": 1544680026, "user_creation_epoch": 1544713200, "user_is_new": 1, "user_token": "6dd4737a8b7ec61313ae5e900420d46815e1d13b2902be71b97a8fbf1f421a3e" }
Edit: This issue is resolved now. The issue was that I had to use a form tag to submit the fetch request. And I have to use FormData as the request option of the fetch request.