tvsatish9
tvsatish9

Reputation: 11

"Content-Type" and "Content-Encoding" headers in axios

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.

  1. "Content-Type": No matter what content-type i pass in request, the content-type in response is always application/JSON. Example Code Snippet:
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.

  1. Content-Encoding: I want to validate the content-encoding in the response, but what i learnt is axios does not return content-encoding header in the response and when i check their github, they are asking to use axios.interceptors. I tried using interceptors but still i am not seeing the header in response. But this header is present in response when i try in POSTMAN. There have been some solution say CORS needs to be enabled in server side. I am strictly asking it from QA point of view because we cannot enable CORS in server side.

Any help is highly appreciable.

Upvotes: 1

Views: 3513

Answers (3)

William Chong
William Chong

Reputation: 2203

  1. 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.

  2. 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

MartinKnopf
MartinKnopf

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

user12475574
user12475574

Reputation:

Try:

axios.post(your-url, {
    headers: {
       'Content-Encoding': 'gzip'
    }
})

or

axios.post(your-url, {
    headers: {
        'Accept-Encoding': 'gzip',
    }
})

Upvotes: 1

Related Questions