React project
POST request by Fetch:
getResource = async () => {
const res = await fetch('https://fanml753mfuuq-uc.a.run.app/api/v1/login/access-token', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, HEAD, OPTIONS'
// 'Authorization': 'Bearer TOKEN',
},
method: 'POST',
mode: 'no-cors',
credentials: 'include',
body: 'grant_type=&username=charl%40mavencodom&password=test&scope=&client_id=&client_secret='
});
// if (!res.ok) {
// throw new Error(`Not Fetch, received ${res.status}`)
// }
const body = await res.json();
return body;
};
I get:
Unhandled Rejection (SyntaxError): Unexpected end of input.
If I uncomment 'if', then I get:
Not Fetch, received 0 .
The request status is 200, but there is no response.
API docs:
Update code with a try/catch and console.log
the error
let response;
try {
response = await fetch(
'https://fan-foto-ml753mfuuq-uc.a.run.app/api/v1/login/access-token',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
// 'Authorization': 'Bearer TOKEN',
},
method: 'POST',
mode: 'no-cors',
// credentials: 'include',
body: 'grant_type=&username=charles%40mavencode.com&password=test&scope=&client_id=&client_secret='
});
} catch (error) {
console.log(error)
// manage errors here
}
console.log(response);
return response;
In the dev tools network tab
the expected results
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1LCJleHAiOjE1OTA1Njk4NTYsInN1YiI6ImFjY2VzcyJ9.SVnzXxc_RMfFwTuzFuK3ul-Vahej_05NpdfPqdt34EY",
"token_type": "bearer"
}