Reputation: 2459
I need to get a cookie value which is returned in the header from my axios post request. So, I make a request to the server and I get a response like this by logging the data to the console. If I console.log data.headers.server
all I get back is 'Apache'.
console.log(data.headers);
The response:
{
date: 'Tue, 11 Aug 2020 17:52:59',
server: 'Apache',
'cache-control': 'private, max-age=0',
'content-type': 'application/json; charset=utf-8',
'x-aspnet-version': '4.0.30319',
'x-powered-by': 'ASP.NET',
'content-length': '85',
'set-cookie': [
'ASP.NET_SessionId=12tz7bfonzkqaywiepwynvm3; path=/; HttpOnly; SameSite=Lax'
],
vary: 'Accept-Encoding',
connection: 'close'
}
I need to get the set-cookie value ie: ASP.NET_SessionId....
Upvotes: 1
Views: 567
Reputation: 3043
You can also access a property of an object this way:
data.headers['set-cookie'];
Then you will need to access the first element, since it is an array:
data.headers['set-cookie'][0];
let data = {
date: 'Tue, 11 Aug 2020 17:52:59',
server: 'Apache',
'cache-control': 'private, max-age=0',
'content-type': 'application/json; charset=utf-8',
'x-aspnet-version': '4.0.30319',
'x-powered-by': 'ASP.NET',
'content-length': '85',
'set-cookie': [
'ASP.NET_SessionId=12tz7bfonzkqaywiepwynvm3; path=/; HttpOnly; SameSite=Lax'
],
vary: 'Accept-Encoding',
connection: 'close'
}
console.log(data['set-cookie'][0]);
In general:
object.prop
//is equivalent to
object["prop"]
Upvotes: 1