My questions seem to be as same as this question: Java equivalent of Pythons urllib.urlencode(HashMap based UrlEncode) however, is not. Because its answer is not right (or I am doing something wrong that I am not aware of).
I have this Python3 code:
def create_vxttoken_data(path_uri, expiry, reuse, shared_secret):
# Combine the parameters into a URL-encoded string
message = []
message.extend([('pathURI', path_uri)])
message.extend([('expiry', expiry)])
message.extend([('reuse', reuse)])
print(message)
url_encoded_message = urlencode(message)
print(url_encoded_message)
# ... rest of the code
This is its output:
[('pathURI', 'http://my.domain.net/*'), ('expiry', 1553937508), ('reuse', 0)]
pathURI=http%3A%2F%2Fmy.domain.net%2F%2A&expiry=1553937508&reuse=0
I tried to write it in Kotlin. So this is my code:
fun main(args: Array<String>) {
val queryParams = "pathURI=$PATH_URI&expiry=${expiryTime(10)}&reuse=$REUSE"
println(queryParams)
val encodedQueryParams = URLEncoder.encode(queryParams, Charsets.UTF_8.toString())
println(encodedQueryParams)
// ... rest of the code
This is its output:
pathURI=http://my.domain.net/*&expiry=1553938196&reuse=0
pathURI%3Dhttp%3A%2F%2Fmy.domain.net%2F*%26expiry%3D1553938196%26reuse%3D0
By compar of the second line of outputs you see that Kotlin/Java has converted =
while Python didn't. Also, Kotlin/Java didn't translate *
while Python does. I want Kotlin/Java generates output like what Python does. The rest of my code is creating a token based on this info. Consequently, my authentication fails.
Note: In my Kotlin/Java file, when I replace
val encodedQueryParams = URLEncoder.encode(queryParams, Charsets.UTF_8.toString())
By
val encodedQueryParams = "pathURI=http%3A%2F%2Fmy.domain.net%2F%2A&expiry=1553937508&reuse=0"
Then my rest code will generate a token which is exactly as same as what Python generates. Therefore, it indicates that something is wrong with my URLEncoder.encode
.