解构 res.headers
Destructure res.headers
我正在尝试解构我的 axios.get 请求的 response.headers,因为我只需要 x-csrf-token。它始终是第二个位置。这是 res.headers 响应的样子
{
'content-type': 'application/json; charset=utf-8',
'x-csrf-token': '17hoqAWEwVmcN32wYgN9WA==',
'cache-control': 'no-cache, no-store',
dataserviceversion: '2.0',
'c4c-odata-response-time': '1195 ms',
date: 'Fri, 28 Feb 2020 10:06:55 GMT',
'transfer-encoding': 'chunked',
connection: 'close, Transfer-Encoding',
'set-cookie': [
'sap-usercontext=sap-client=041; path=/;HttpOnly;Secure',
'
],
'strict-transport-security': 'max-age=31536000 ; includeSubDomains'
}
我试过了
let{a,b} = res.headers;
console.log(b);
和
let[,b] = res.headers;
console.log(b);
但我刚刚得到:undefined 不是函数
It is always the second position
这与对象解构无关。您使用的是键,而不是位置。
获取方式:
const {'x-csrf-token': token} = res.headers;
或
const {headers: {'x-csrf-token': token}] = res;
实例:
const res = {
headers: {
'content-type': 'application/json; charset=utf-8',
'x-csrf-token': '17hoqAWEwVmcN32wYgN9WA==',
'cache-control': 'no-cache, no-store',
dataserviceversion: '2.0',
'c4c-odata-response-time': '1195 ms',
date: 'Fri, 28 Feb 2020 10:06:55 GMT',
'transfer-encoding': 'chunked',
connection: 'close, Transfer-Encoding',
'set-cookie': [
'sap-usercontext=sap-client=041; path=/;HttpOnly;Secure'
],
'strict-transport-security': 'max-age=31536000 ; includeSubDomains'
}
};
const {'x-csrf-token': token} = res.headers;
console.log(token);
const {headers: {'x-csrf-token': token2}} = res;
console.log(token2);
这里的关键是,解构语法是对象字面量的逆向,只不过不是key: value
表示"put value
in for property key
",而是表示"take the value from property key
and put it in value
"——也就是说,信息在文字从右向左流动,但解构中的信息从左向右流动。这是我新书第七章的图(详见我的简介);
在这种特殊情况下,与
相比,解构不会给你带来太多好处
const token = res.headers['x-csrf-token'];
我正在尝试解构我的 axios.get 请求的 response.headers,因为我只需要 x-csrf-token。它始终是第二个位置。这是 res.headers 响应的样子
{
'content-type': 'application/json; charset=utf-8',
'x-csrf-token': '17hoqAWEwVmcN32wYgN9WA==',
'cache-control': 'no-cache, no-store',
dataserviceversion: '2.0',
'c4c-odata-response-time': '1195 ms',
date: 'Fri, 28 Feb 2020 10:06:55 GMT',
'transfer-encoding': 'chunked',
connection: 'close, Transfer-Encoding',
'set-cookie': [
'sap-usercontext=sap-client=041; path=/;HttpOnly;Secure',
'
],
'strict-transport-security': 'max-age=31536000 ; includeSubDomains'
}
我试过了
let{a,b} = res.headers;
console.log(b);
和
let[,b] = res.headers;
console.log(b);
但我刚刚得到:undefined 不是函数
It is always the second position
这与对象解构无关。您使用的是键,而不是位置。
获取方式:
const {'x-csrf-token': token} = res.headers;
或
const {headers: {'x-csrf-token': token}] = res;
实例:
const res = {
headers: {
'content-type': 'application/json; charset=utf-8',
'x-csrf-token': '17hoqAWEwVmcN32wYgN9WA==',
'cache-control': 'no-cache, no-store',
dataserviceversion: '2.0',
'c4c-odata-response-time': '1195 ms',
date: 'Fri, 28 Feb 2020 10:06:55 GMT',
'transfer-encoding': 'chunked',
connection: 'close, Transfer-Encoding',
'set-cookie': [
'sap-usercontext=sap-client=041; path=/;HttpOnly;Secure'
],
'strict-transport-security': 'max-age=31536000 ; includeSubDomains'
}
};
const {'x-csrf-token': token} = res.headers;
console.log(token);
const {headers: {'x-csrf-token': token2}} = res;
console.log(token2);
这里的关键是,解构语法是对象字面量的逆向,只不过不是key: value
表示"put value
in for property key
",而是表示"take the value from property key
and put it in value
"——也就是说,信息在文字从右向左流动,但解构中的信息从左向右流动。这是我新书第七章的图(详见我的简介);
在这种特殊情况下,与
相比,解构不会给你带来太多好处const token = res.headers['x-csrf-token'];