Or Assayag
Or Assayag

Reputation: 6346

JavaScript - Encode characters and URI malformed error

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);

Upvotes: 2

Views: 3141

Answers (1)

Or Assayag
Or Assayag

Reputation: 6346

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))

Upvotes: -1

Related Questions