Morgana
Morgana

Reputation: 337

encodeURIComponent and content-type: 'charset: utf-8'

I'm building a react application and I use Node (with Express) as a proxy-server. I send data from react app to node-express, then in Node I use that data to form URI and to make requests to another server.

My question is this: Shouldn't 'content-type': 'charset: utf-8' be enough when I send data including greek characters to Node? For example, I make a post request (using Fetch) to Node and I send code 'ΠΕ0001' using the header I already mentioned. Why do I get the error 'Path contains unescaped characters'? When I use encodeURIComponent it does work, but why 'charset: utf-8' is not enough?

Upvotes: 1

Views: 771

Answers (2)

Tobias Pfandzelter
Tobias Pfandzelter

Reputation: 357

Just setting the header 'content-type': 'charset: utf-8' is not enough. Essentially with this Header you're just telling the server (Node in this instance), that the data you send is in utf-8 format, which it should expect anyway.

Your string, however, is in UTF-16 format, because the letter Π needs two bytes to be represented..

You can read more about character encoding here.

Hence you need encodeURIComponent first. In our case, Π is then represented as %CE%A0, which are its byte's representations in UTF-8.

Upvotes: 2

Muhammad Kamran
Muhammad Kamran

Reputation: 109

  1. use this method JSON.stringify() to convert data to json object.
  2. then pass that json object into encodeURIComponent()
  3. then call fetch method

Upvotes: 0

Related Questions