1

I have a problem with encoding a part of and URL using URLEncoder.

Let's say that I have:

override suspend fun getData(ids: String): SomeData {
    val itemIds = ids.split(",").iterator()
    val sb = StringBuilder()
    sb.append("/v1/data?ids=")
    itemIds.forEach {
        val assetsJson = myObjectMapperWrapper.getObjectMapper().writeValueAsString(
            DataRequest(type = "Picture", id = it)
        )
        sb.append(URLEncoder.encode(assetsJson, Charsets.UTF_8))
        if (itemIds.hasNext()) sb.append("&data=")
    }
    val uri = sb.toString()

assetsJson looks, for example, like this: {"type":"Picture","id":" 12345"} (spaces in the id field are intentional, as this is a String and needs those spaces for reasons unknown (this is a shot to an API).

Encoding this string using an online tool gives me this:

%7B%22type%22%3A%22Picture%22%2C%22id%22%3A%22%20%20%20%20%20%20%20%20%2012345%22%7D

It starts with %7B%22 and the spaces are represented by %20. Pasting this to the URI that is used to call the API in Insomnia is working.

Using a debugger I've found out that: is encoded as:

%7B%22type%22%3A%22Picture%22%2C%22id%22%3A%22+++++++++12345%22%7D

So no %20 for a space character, but + signs.

The last problem is what is really sent to the server:

%257B%2522type%2522%253A%2522Picture%2522%252C%2522id%2522%253A%2522+++++++++12345%2522%257D

It's %257B%2522 instead of %7B&22 and + instead of %20. And this is not working even in Insomnia, and the API server returns:

Failed to convert value of type 'java.lang.String[]' to required type 'java.util.Set'; nested exception is java.lang.IllegalArgumentException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (String)"%7B%22type%22%3A%22Picture%22%2C%22id%22%3A%22 12345%22%7D"; line: 1, column: 2]

What's noticeable, the space character looks normal: instead of + like it was created by StringBuilder or %20 like it was created by the online tool.

The string in this stack trace is %7B%22type%22%3A%22Picture%22%2C%22id%22%3A%22 12345%22%7D" which is close to this: >%7B%22type%22%3A%22Picture%22%2C%22id%22%3A%22%20%20%20%20%20%20%20%20%2012345%22%7D but there is only one space instead of 9, and it is represented by a regular space instead of %20 or +.

How can I fix this? Am I doing something wrong?

hc0re
  • 1,806
  • 2
  • 26
  • 61
  • Related: https://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character – Joffrey Dec 02 '22 at 12:15

0 Answers0