Hope you all are well and good. There is one thing that I want to discuss with you all and get some response. I am calling a REST API from JavaScript with POST operation using Async/Await functionality. Now, Async call is successful but I need the response that I have received from REST endpoint for further operations. So, how can I store/use the response that I received from REST endpoint (POST) when I am making asynchronous call to REST endpoint? Is there a way for me to store the response that I have received from REST endpoint for POST call?
I have used below code:
function callRESTEndpoint (requestPayload) {
let restURI = "url";
let head = new Headers();
let encoded = window.btoa('username:password');
let authorization = 'Basic ' + encoded;
head.append('Content-Type', 'application/json');
head.append('Authorization', authorization);
let request = new Request(restURI, {
method: 'POST',
headers: head,
mode: 'cors',
body: JSON.stringify(requestPayload);
credentials: 'same-origin'
});
let restResponse = JSON.stringify(callRESTAsynchronously(request));
console.log('rest response: ', restResponse);
}
async function callRESTAsynchronously(requestReceived){
try{
const JSONResponse = await (await fetch(requestReceived)).json();
console.log('response: ', JSONResponse);
return JSONResponse;
} catch(err) {
return err;
}
}