Closed mvasin closed 5 years ago
Hi and thanks for the awesome lib!
Hi @mvasin, thanks!
Is there a way to get response headers when using wretch? I wasn't able to find anything in the docs.
Yes, you can get the headers them by using .res()
to retrieve the raw fetch response object.
// see it in action here: https://runkit.com/embed/ydiucqoe45xu
wretch('...').get().res(res => {
// 'res' is the raw response (see https://developer.mozilla.org/en-US/docs/Web/API/Response)
// 'res.headers' contains the Headers (https://developer.mozilla.org/en-US/docs/Web/API/Headers)
console.log(Array.from(res.headers.entries()))
return res.text()
}).then(console.log)
What is the best way to pass headers further alongside with json data to resolve in then block ?
const successHandler = response => {
console.log({response})
if (response.status === 201) {
let authToken = response.data["auth-token"];
let { client } = response.data;
let sessionCookie = response.headers["set-cookie"][0];
onSuccess(authToken, sessionCookie, client);
} else {
errorHandler();
}
};
If you need both headers and the json data, here's how I did it:
.res(response => {
// do something with response.headers
return res.json(); // or res.text() if you need text
})
.then(json => {...})
I had the same question myself and just wanted to att that this works response.headers.get('location')
Hi and thanks for the awesome lib!
Is there a way to get response headers when using wretch? I wasn't able to find anything in the docs.