CodeWithYou

AxiosError, Parse Error, Invalid header value char

Published on
Authors
AxiosError, Parse Error, Invalid header value char

AxiosError: Parse Error: Invalid header value char

Today I was working on a project that uses Axios in Node.js. I was trying to make a request to an API, but I was getting this error:

AxiosError: Parse Error: Invalid header value char

The code I was using was:

import axios from 'axios'

const url = 'https://www.city.anjo.aichi.jp/shisei/shinchaku.xml'

axios.get(url).then((response) => {
  console.log(response.data)
})

After some hours of debugging, I found out that the problem was that the API was returning a response with a header that had a value with a character that was not allowed.

Node.js' HTTP parser is stricter than parsers used by web browsers, which prevents scraping of websites whose servers do not comply with HTTP specs, either by accident or due to some anti-scraping protections, causing e.g. the invalid header value char error.

The solution was to use the insecureHTTPParser option:

import axios from 'axios'

const url = 'https://www.city.anjo.aichi.jp/shisei/shinchaku.xml'

axios.get(url, { insecureHTTPParser: true }).then((response) => {
  console.log(response.data)
})
Advertisement

The insecureHTTPParser option forces Axios to use the Node.js HTTP parser, which is less strict than the default one. This option is not recommended, but it's useful when you need to make a request to an API that is not following the HTTP specs. However, it will also open your application to some security vulnerabilities, although the risk should be negligible as these vulnerabilities mainly relate to server applications, not clients.

You can read more about this in the blog post.

Conclusion

This is a very simple post, but I hope it will help you doesn't spend hours debugging like I did. If you have any questions, please leave a comment below.

Thanks for reading!

References

Advertisement