[英]How to get content-type from the response headers with Fetch
我正在嘗試從我的GET
請求中訪問返回的content-type
,以便我可以決定我想要的預覽類型html
可能通過 iframe 和 ZBCD1B68617759B1DFCFF0403A6B5A8D1 可能是一些查看器。 問題是當我執行console.log(response.headers)
時,返回的 object 中沒有內容類型,但是當我檢查網絡選項卡時,響應標頭有內容類型:html/text。 如何從響應標頭中獲取內容類型? 這就是我的 GET 請求的樣子
const getFile = async () => {
var requestOptions = {
method: "GET",
headers: context.client_header,
redirect: "follow",
};
let statusID = context.currentStatus.ApplicationID;
var response = await fetch(
process.env.REACT_APP_API_ENDPOINT +
"/services/getStatus?ApplicationID=" +
statusID,
requestOptions
);
console.log(response.headers);
if (response.ok) {
let fileHtml = await response.text();
setfileURL(fileHtml);
} else {
alert.show("Someting went wrong");
}
};
標Headers
object 不是console.log()
的理想選擇,因為它不容易序列化。
如果您想查看其中的所有內容,請嘗試通過擴展語法將其分解為條目
console.log(...response.headers)
您可能會發現您實際上可以通過以下方式訪問您想要的內容
response.headers.get("content-type")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.