mohamedali10
mohamedali10

Reputation: 61

Play 2.6: Encoding url parameters

I have something like the following URL:

http://www.123.com/api/link/http://www.bla.com/?contenId=123&User=test

But i know it needs to be encoded to be valid. Is there a way to encode it to be on the right format, something like the following:

http://www.123.com/api/link/http%3a%2f%2fwww.bla.com%2f%3fcontenId%3d123%26User%3dtest

Upvotes: 1

Views: 2732

Answers (1)

o-0
o-0

Reputation: 1789

You can use import java.net.URLEncoder for encoding URLs and/or import java.netURLDecoder for decoding the encoded urls.

For encoding URL:

import java.net.URLEncoder
val urlEncoded = URLEncoder.encode("https://encodeMe", "UTF-8")

For decoding URL:

import java.net.URLDecoder
val urlDecoded = URLDecoder.decode("http%3a%2f%2fwww.bla.com", "UTF-8")

Also look at the following note, in the library, regarding the UTF-8.

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Upvotes: 3

Related Questions