kotlinkotlin-multiplatformktorktor-client

Ktor GET request with multipart body


I'm trying to make a GET request that has a multipart body in Ktor. However I get a server error 500 when I'm doing the following code:

client.get {
        url { path("the", "path") }
        setBody(MultiPartFormDataContent(
            formData {
                append("clientId", clientId)
                append("code", code)
            }
        ))
}.body<Response>()

If I leave the body out, the server response with a missing body error.

If I do the same call in curl, it succeeds:

curl -X GET 'https://server.url/api/v1/the/path' \
  -d 'code=code' \
  -d 'clientId=clientId'

It is a third party server where I don't have any control, therefore I have to make a GET request with a data body. When I log the call I can see that indeed there is a body sent to the server:

HttpClient: REQUEST: https://server.url/api/v1/the/path
METHOD: HttpMethod(value=GET)
COMMON HEADERS
-> Accept: application/json
-> Accept-Charset: UTF-8
CONTENT HEADERS
-> Content-Length: 372
-> Content-Type: multipart/form-data;     boundary=69e936b8-4a7c7093-16bba07f-19d814e2708194c1-3248703532ce82f9-57eea5437
BODY Content-Type: multipart/form-data;     boundary=69e936b8-4a7c7093-16bba07f-19d814e2708194c1-3248703532ce82f9-57eea5437
BODY START
--69e936b8-4a7c7093-16bba07f-19d814e2708194c1-3248703532ce82f9-57eea5437
Content-Disposition: form-data; name=clientId
Content-Length: 8

clientId
--69e936b8-4a7c7093-16bba07f-19d814e2708194c1-3248703532ce82f9-57eea5437
Content-Disposition: form-data; name=code
Content-Length: 4

code
--69e936b8-4a7c7093-16bba07f-19d814e2708194c1-3248703532ce82f9-57eea5437--

BODY END

Why is it failing with my Ktor call, but succeeds when doing it in curl?


Solution

  • If you are using GET, you should be able to add the parameters code and clientId as query parameters in the URL, instead of sending these in the body. The server should accept that.