Hello, I’m trying to make a POST request with headers and data.
inline fun <reified T> post(url: String, headersMap: Map<String, String>, data: String) = runBlocking {
return@runBlocking httpClient.post<T>(url) {
headers {
headersMap.forEach { append(it.key, it.value) }
}
body = TextContent(
text = data,
contentType = ContentType.Application.Json
)
}
}
I think, there could be future optimizations with runBlocking
and headersMap.forEach
but currently I’m not sure if all is ok with sending request data here. My data is a JSON-like String but maybe it’s not the valid way to make it this way: data = "{\"page\":$page, \"page_size\": $pageSize}"
Anyway, I got a response like body data was not used. What is the correct method to send the data in Ktor?
The Python
method:
requests.post(url, headers=request_data, data=json.dumps(request_json)).json()
where request_data is a dictionary
The curl
method looks like this:
curl 'https://www.url/' \
-H 'Content-Type: application/json' \
-H 'Referer: https://url/' \
--data-raw "{"page":2, "page_size":50}" \
--compressed
P.S. With this structure I got Serializer for class 'TextContent' is not found.
SerializationException