I’m trying to access the returned content-type
from my GET
request so I can decide the kind of preview I want to like for html
maybe pass through an iframe and for a PDF maybe some viewer. The problem is when I do console.log(response.headers)
the object returned doesn’t have content-type in it but when I check the networks tab the response headers has content-type:html/text. How can I get the content-type from the response headers?
this is how my GET request looks like
JavaScript
x
24
24
1
const getFile = async () => {
2
var requestOptions = {
3
method: "GET",
4
headers: context.client_header,
5
redirect: "follow",
6
};
7
let statusID = context.currentStatus.ApplicationID;
8
var response = await fetch(
9
process.env.REACT_APP_API_ENDPOINT +
10
"/services/getStatus?ApplicationID=" +
11
statusID,
12
requestOptions
13
);
14
15
console.log(response.headers);
16
17
if (response.ok) {
18
let fileHtml = await response.text();
19
setfileURL(fileHtml);
20
} else {
21
alert.show("Someting went wrong");
22
}
23
};
24
Advertisement
Answer
The Headers
object isn’t a great candidate for console.log()
since it is not easily serialisable.
If you want to see everything in it, try breaking it down to its entries via spread syntax
JavaScript
1
2
1
console.log(response.headers)
2
You’ll probably find that you can in fact access what you want via
JavaScript
1
2
1
response.headers.get("content-type")
2
See Headers.get()