1

I recently have a bug, when I try to send into my node server a query search of the following:
!@#$%^&*()_+
I get an error on my Chrome:

{status: 500, message: "URI malformed"}
message: "URI malformed"
status: 500

After reading:
When are you supposed to use escape instead of encodeURI / encodeURIComponent?
Javascript decodeURI(Component) malformed uri exception
any way of making encodeURIComponent in javascript ignore certain characters?
Percent encoding javascript
Pass a percent (%) sign in a url and get exact value of it using php
Javascript decodeURI(Component) malformed uri exception

My code still won't work for: !@#$%^&*()_+ when I wrap my string with encodeURIComponent and in the server do decodeURIComponent.
It also does not work with encodeURL.
If I do manually replace I get %2525 and it's not good.
Any idea how to solve it?
Thanks.

My code:

const endpoint = `${endpoint}&text=${encodeURIComponent(query)}`;
await fetch(endpoint);
Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • 1
    tha't might be a double-encoding problem. `%2525` happens when you first encode the percent sign `%` => `%25`, and then re-encode that: `%25` => `%2525`. It's better explained here: https://stackoverflow.com/questions/29249900/extra-escape-character-in-go-url – GrafiCode Jun 26 '22 at 17:23
  • Can you provide the code snippet that builds the URL and launches the request? You should use `encodeURIComponent`, but probably you are doings something wrong in that process. – trincot Jun 26 '22 at 17:29
  • That code doesn't give me the chrome error. – trincot Jun 26 '22 at 19:15
  • 1
    What is `endpoint`? What is `query`? And how do you apply `decodeURIComponent` in the server? – Bergi Jun 27 '22 at 10:55

1 Answers1

-1

Eventually there was a bug on my server side. I tried to decode the '%' sign. Solved it by the following:

On the client:

const endpoint = `${endpoint}&text=${encodeURIComponent(query)}`;
await fetch(endpoint);

On the server:

decodeURIComponent(encodeURIComponent(query.text))
Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • 1
    That's rather pointless. Just use `query.text` directly. – Bergi Jun 27 '22 at 10:55
  • @bergi I edited the code. If you have other way to pass '!@#$%^&*()_+' to the server from the client I would be happy to hear. – Or Assayag Jun 27 '22 at 18:38
  • 1
    As I said, just directly use `query.text` on the server. – Bergi Jun 27 '22 at 18:40
  • @bergi but then I will get an error if I will try to send '#' or '&' to the server. – Or Assayag Jun 27 '22 at 18:42
  • 1
    No you won't. If you would, you'd also be getting the error when getting `query.text` and applying the pointless `decodeURIComponent(encodeURIComponent(…))` to it. – Bergi Jun 27 '22 at 18:43