admin管理员组文章数量:1430114
I'm calling API endpoint using fetch API
.
How can I read response body and headers in resolved body promise?
My code snippet below:
fetch(url, {
credentials: 'include',
method: 'post',
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
email: email,
password: password,
}),
})
.then(response => response.json())
.then(function(response) {
// How to access response headers here?
});
I'm calling API endpoint using fetch API
.
How can I read response body and headers in resolved body promise?
My code snippet below:
fetch(url, {
credentials: 'include',
method: 'post',
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
email: email,
password: password,
}),
})
.then(response => response.json())
.then(function(response) {
// How to access response headers here?
});
Share
Improve this question
edited Aug 28, 2023 at 5:56
VLAZ
29.2k9 gold badges63 silver badges84 bronze badges
asked May 22, 2016 at 10:26
LukasMacLukasMac
8981 gold badge8 silver badges20 bronze badges
1 Answer
Reset to default 4As described in the Fetch API documentation, you can get response headers with this snippet:
fetch(myRequest)
.then((response) => {
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
throw new TypeError("Oops, we haven't got JSON!");
}
return response.json();
})
.then((data) => {
/* process your data further */
})
.catch((error) => console.error(error));
For the body you will find here some examples.
本文标签: javascriptHow to read headers in bodyresponse promiseStack Overflow
版权声明:本文标题:javascript - How to read headers in bodyresponse promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745509363a2661394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论