1

I have problem with send Params in body request. On Postman I have successful request but using Volley I only get Unexpected response code 415 error code.

Postman successful request

Volley request

2 Answers2

0

Postman automatically generates some random boundary text that gets added to the body params when sending the request. You are missing that on your code. Here's how you might do it:

Use a random boundary that the server will use to split the params

val BOUNDARY = "AS24adije32MDJHEM9oMaGnKUXtfHq"
val MULTIPART_FORMDATA = "multipart/form-data;boundary=" + BOUNDARY

getBodyContentType function should return that MULTIPART_FORMDATA

override fun getBodyContentType(): String {
    return MULTIPART_FORMDATA
}

On the getBody() function, add the boundary to the params like this:

override fun getBody(): ByteArray {
    val params = HashMap<String, String>()
    params.put("profile_id", "1")
    params.put("place_name", "La la land")
    params.put("place_identifier", "10239jodmda")

    val map: List<String> = params.map {
        (key, value) -> "--$BOUNDARY\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n"
    }
    val endResult = "${map.joinToString("")}\n--$BOUNDARY--\n"
    return endResult.toByteArray()
}

Since you are already setting up the content type on getBodyContentType(), you probably don't need the following line on getHeaders():

headers.put("Content-Type", "multipart/form-data")

This SO answer shows how to do something similar in java: https://stackoverflow.com/a/38238994/3189164

spuente
  • 314
  • 5
  • 16
0

The solution that worked for me is remove the line that setting Content-Type from header!