1

I'm trying to send Japanese characters in the headers but getting the error Invalid character in header content.

I'm using Axios to make the HTTP call and this is what the request configuration looks like:

let request = {
    url: 'http://x.x.x.x:3003/resource/path',
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'header1': '星'
    },
    params: {
        '星': '星',
    }
}

Response: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["header1"]

I found this post which I think is very similar to what I'm trying to achieve here, but the solution proposed is limited to Authorization only. In my use-case, I can have Japanese characters in both key and value. Consider them as custom headers for which we don't have any prefined key or value.

I tried to pass application/json; charset=utf-8 as the value for Content-Type but that also didn't work. I also tried encoding the value with encodedURIComponent but the server received the header as header1: '%E6%98%9F'. Decoding the received header at the server's end would solve the issue but unfortunately, a fix like this is beyond my reach.

I'm facing this issue in the case of headers only. Surprisingly, params are getting passed correctly. I tried to find the reason behind this and stumbled upon this document. The note NOTE: Express automatically decodes the values in req.params (using decodeURIComponent). distinctly mentions express decoding the received encoded params. Why isn't there such an option for headers as well?

Can anyone guide me in the right direction? If what I'm trying to achieve is not possible then what other alternatives do I have?

Cheers!

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
  • 1
    Did you get the error when inspecting the response from express? I just tried in my local machine and it works for both side. https://github.com/Darkripper214/axios-test – PKiong Mar 19 '21 at 20:11
  • You're sending the `request` as body data, not as the request configuration. If you notice in the logs, the `headers` are coming in `req.body` not `req.headers`. Format for `axios.post` is `axios.post(url[, data[, config]])`. – Kartik Chauhan Mar 21 '21 at 07:06

0 Answers0