UPDATED
I am trying to get the Date from my response headers but only Content-Type
is returned. Is it possible to get the date?
Here are my response headers after making a POST request:
Here is my utility function for making the POST request:
export default async function postData(url, func, audience, requestObj) {
const accessToken = await func({
audience: audience,
});
const myHeaders = new Headers();
myHeaders.append('authorization', `Bearer ${accessToken}`);
myHeaders.append('Content-Type', 'application/json');
const raw = JSON.stringify(requestObj);
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
};
const response = await fetch(url, requestOptions);
const transactionDate = response.headers.get('Date');
//This returns null
console.log('Date', transactionDate);
//This returns only the Content-Type
for (let pair of response.headers.entries()) {
console.log(pair[0] + ': ' + pair[1]);
}
if (!response.ok) {
if (response.status >= 500 && response.status <= 599) {
throw new Error(
'A server error occurred and we were unable to submit your data.'
);
} else if (response.status >= 400 && response.status <= 499) {
const text = await response.text();
throw new Error(text);
} else {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
const result = await response.json();
return result;
}
This is the end of postData
:
const result = await response.json(); return result;
It doesn't return the response. It parses the response body as JSON and returns the resulting data structure.
That data structure doesn't include the headers. They exist on the response
object.
If you want that data outside of postData
then you'll need to read that value (response.headers
) there and return it (possibly as part of an array or object that also includes the value of result
).