4

I have a string in JS side which is url.QueryEscaped.

Spaces were replaced with + sign by url.QueryEscape. They don't get converted back to space in decodeURIComponent. Should I manually do a string replace all + with space? What is the right way to decode it?

1

2 Answers 2

2

One simple method is to replace all the + characters with spaces prior to decoding. For example:

decodeURIComponent("%2f+%2b".replace(/\+/g, " "))

will correctly decode the string to "/ +". Note that it is necessary to perform the replacement prior to decoding, since there could be encoded + characters in the string.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, thats what I did, but was wondering if I will miss some other encoded characters like space
The Go QueryEscape function just encodes spaces as plus, and uses percent encoding for other characters not valid in a URL query component. So replace and decodeURIComponent on the JS side should handle everything.
Just a note, the correct way to do a replace all would be to enclose the search string in a RegExp global modifier like so decodeURIComponent("%2f+%2b".replace(/\+/g, " "))
1

If you control Go side, use url.PathEscape and then you can simply use decodeURIComponent without anything extra.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.