0

I am trying to encode a URL parameter.

For example when I am encoding

qOddENxeLxL+13drGKYUgA==\n

using URL Encoder tool

It gives the following output which works when I request API

qOddENxeLxL%2B13drGKYUgA%3D%3D%5Cn

But when I am encoding URL from my Java code (Android) using URLEncoder.encode("qOddENxeLxL+13drGKYUgA==\n", "UTF-8");

It gives me the following result

qOddENxeLxL%252B13drGKYUgA%253D%253D%250A

I tried using other Encoding schemes too but could not produce the same result.

0

3 Answers 3

1

The issue is because the \n is being interpreted as a new line character. Java will treat \ inside a string as starting an escape sequence. You have to escape it in order to get the same thing as in the URL you provided.

System.out.println(URLEncoder.encode("qOddENxeLxL+13drGKYUgA==\\n", "UTF-8"));

This will provide the same result:

qOddENxeLxL%2B13drGKYUgA%3D%3D%5Cn
Sign up to request clarification or add additional context in comments.

Comments

1

The issue is that you are feeding \n to the URLEncoder tool, which doesn't understand it as an escape sequence and so gives you %5Cn, and to the Java compiler inside a string literal, which does understand it and so gives you 0x0A.

Comments

-1

Figured out the issue, here string was getting encoded two times.

While passing parameter to Retrofit call it is getting encoded automatically by retrofit and I was passing encoded parameter to retrofit so it got encoded again.

BTW thanks for the explanations. :)

2 Comments

No. If that was the issue you would have no % characters and multiple %25 characters.
yes there are multiple %25 in qOddENxeLxL%252B13drGKYUgA%253D%253D%250A

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.