1

I'm having an issue with a NodeJS REST api created using express. I have two calls, a get and a post set up like this:

router.get('/:id', (request, response) => {
    console.log(request.params.id);
});
router.post('/:id', (request, response) => {
    console.log(request.params.id);
});

now, I want the ID to be able to contain special characters (UTF8). The problem is, when I use postman to test the requests, it looks like they are encoded very differently:

GET http://localhost:3000/api/â outputs â

POST http://localhost:3000/api/â outputs â

Does anyone have any idea what I am missing here? I must mention that the post call also contains a file upload so the content type will be multipart/form-data

Alex Popescu
  • 71
  • 1
  • 8

3 Answers3

2

You should encode your URL on the client and decode it on the server. See the following articles:

For JavaScript, encodeURI may come in handy.

Ryan H.
  • 7,374
  • 4
  • 39
  • 46
  • actually, in this case I wanted the special characters to be in the url because there will be valid words in various languages. SO, I wanted them to be indexable and have the clear word in there – Alex Popescu Apr 06 '18 at 19:13
  • 1
    _NEVER_ use encodeURI, encodeURI guesses where your parameters starts and ends, it has no reliable way of knowing when you want to encode an actual `&`, and when you want to end a parameter and begin a new 1 (which is separated by `&`), never use it, always use encodeURIComponent instead, which is reliable and doesn't do any guessing. – hanshenrik Apr 06 '18 at 19:26
  • I set up a quick node/express server on a macOS platform to test this and was able to successfully `console.log` the correct character for both GET and POST requests from a Postman client. I guess if UTF-8 character support for URL parameters is not part of the spec, then the results are going to be unpredictable. Maybe try re-encoding with `node-iconv`: https://stackoverflow.com/a/14114534/305383 – Ryan H. Apr 08 '18 at 22:07
0

It looks like postman does UTF-8 encoding but NOT proper url encoding. Consequently, what you type in the request url box translates to something different than what would happen if you typed that url in a browser.

I'm requesting: GET localhost/ä but it encodes it on the wire as localhost/ä (This is now an invalid URL because it contains non ascii characters)

But when I type localhost/ä in to google chrome, it correctly encodes the request as localhost/%C3%A4

So you could try manually url encoding your request to http://localhost:3000/api/%C3%A2

In my opinion this is a bug (perhaps a regression). I am using the latest version of PostMan v7.11.0 on MacOS.

Phil
  • 1,996
  • 1
  • 19
  • 26
-1

Does anyone have any idea what I am missing here?

yeah, it doesn't output â, it outputs â, but whatever you're checking the result with, think you're reading something else (iso-8859-1 maybe?), not UTF-8, and renders it as â

Most likely, you're viewing the result in a web browser, and the web server is sending the wrong Content-Type header. try doing header("Content-type: text/plain;charset=utf-8"); or header("Content-type: text/html;charset=utf-8"); , then your browser should render your â correctly.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • I am using Postman to make the two calls and the output I'm talking about is the `console.log(request.params.id)` from NodeJS. The get call has `Content-Type: application/json` in the header and the post call is `multipart/form-data` but none of them set an explicit charset in the request. The resulting difference in values in on the server side – Alex Popescu Apr 06 '18 at 19:14