Reputation: 11
I am using [email protected] and I want to validate the response headers. I am unable validate the headers "Content-Type" and "Content-Encoding" from a GET response.
if (<token is present>) {
request.headers = {
authorization : 'Bearer ${token}'
}
} else {
config.auth = {}
}
config.headers = Object.assign(config.header, {
'content-type': application/<custom content>,
'accept-encoding': 'gzip, deflate, br'
}
await axios.get(endPoint, config)
.then(response => {
return response
}*
When i am checking response.header, i see that content-type is showing as "application/json" instead of the custom type. But when i hit the same url in POSTMAN i could see that content-type is as expected.
Any help is highly appreciable.
Upvotes: 1
Views: 3513
Reputation: 2203
Content-Type
means the data type of the request/response body fired, setting Content-Type
in a GET request is meaningless since there is no request body.
If you want to get X
Content-Type as response, you should be setting Accept
header i.e. Accept: X
in request instead, assuming your server is compliance with the related HTTP specifications.
You would need to set Accept-Encoding
in your request since Content-Encoding
normally is only set if the response is compressed. Most http server check for Accept-Encoding
in request to know if they can send compressed response.
Upvotes: 0
Reputation: 105
This is by design: https://axios-http.com/docs/req_config
I also ran into this and couldn't find a solution. Ended up using node-fetch instead.
Upvotes: 0
Reputation:
Try:
axios.post(your-url, {
headers: {
'Content-Encoding': 'gzip'
}
})
or
axios.post(your-url, {
headers: {
'Accept-Encoding': 'gzip',
}
})
Upvotes: 1