How do I send json in form data from the ktor clie...
# ktor
d
How do I send json in form data from the ktor client and receive it on a ktor server? I need multiple keys (json and json2) with json data in them in the same request.
b
d
It seems like submitForm and recieveParameters doesn't work (even if I take care of serialization/deserialization)
And that's all that seems to be documented... I even tried:
Copy code
submitFormWithBinaryData(
                url = "/",
                formData = listOf(
                    PartData.FormItem(
                        Json.encodeToString(fooJsonString),
                        {},
                        partHeaders = headersOf(
                            HttpHeaders.ContentType to listOf(ContentType.Application.Json.let { "${it.contentType}/${it.contentSubtype}" })
                        )
                    )
                )
            )
with:
Copy code
val formParameters = call.receiveMultipart()
Json.decodeFromString((formParameters.readPart() as PartData.FormItem).value)
and that also doesn't work... (and doesn't give me the ability to name the form parts either...)
a
Please have a look at this example.
The first parameter for the
append
is the name
d
I just noticed that (even though the name is uploading a file), but it doesn't allow specifying a content type, and how do I receive that on the server side as the object it started off as?
I see that there's
Copy code
@InternalAPI
public final fun <T : Any> append(
    key: String,
    value: T,
    headers: Headers
): Unit
but it's internal api... so I need to handle serialization/deserialization myself?
👌 1
It might one day be nice to add an append() and a way to receive such a serialized/deserialized object using the installed ContentNegotiation...
But thanks! Now it seems to be working.
695 Views