Sudhanshu Singh
08/05/2023, 9:07 AMHttpClient(OkHttp) {
engine {
preconfigured = okHttpClient
}
defaultRequest {
url("<http://host.com|host.com>")
// this adds content-type = application/json to all requests
contentType(ContentType.Application.Json)
}
now in one of the request i want to change the content-type
as application/xml
client.get("endpoint"){
// doing this adds two content-type json and xml
headers.append("content-type", "application/xml")
// this also adds two typos
headers["content-type"] = acceptHeader
}
s3rius
08/05/2023, 3:01 PMheaders.set()
ought to do it.Sudhanshu Singh
08/05/2023, 5:23 PMs3rius
08/05/2023, 6:10 PM@Test
fun `test requests`() = runTest {
val engine = MockEngine { respondOk() }
val client = HttpClient(engine) {
defaultRequest {
contentType(ContentType.Application.Json)
}
}
// using default param
client.get<String>("url")
engine.requestHistory.last().headers.apply {
get(HttpHeaders.ContentType) shouldBeEqualTo "application/json"
}
// overwriting param
client.get<String>("url") {
headers.set(HttpHeaders.ContentType, "application/xml")
}
engine.requestHistory.last().headers.apply {
get(HttpHeaders.ContentType) shouldBeEqualTo "application/xml"
}
}
s3rius
08/05/2023, 6:12 PMheaders.append()
in the code? Or have another piece of code that might add such a header?Sudhanshu Singh
08/05/2023, 7:18 PM@Test
fun `test requests`() = runTest {
val engine = MockEngine { respondOk() }
val client = HttpClient(engine) {
defaultRequest {
// send request body as json
contentType(ContentType.Application.Json)
headers.appendIfNameAbsent(HttpHeaders.AcceptLanguage, Locale.getDefault().language)
}
}
// using default param
client.get("url")
engine.requestHistory.last().headers.apply {
assertEquals(get(HttpHeaders.ContentType), "application/json")
}
// overwriting param
client.get("url") {
headers.set(HttpHeaders.ContentType, "application/xml")
}
engine.requestHistory.last().headers.apply {
assertEquals(get(HttpHeaders.ContentType), "application/xml")
}
}
Sudhanshu Singh
08/05/2023, 7:18 PMexpected: <application/json> but was: <application/xml>
Expected :application/json
Actual :application/xml
Sudhanshu Singh
08/05/2023, 7:18 PMs3rius
08/05/2023, 9:02 PMs3rius
08/05/2023, 9:22 PMRustam Siniukov
08/08/2023, 12:20 AMcontentType(ContentType.Application.Json)
inside defaultRequest {}
block with headers.appendIfNameMissing(HttpHeaders.ContentType, ContentType.Application.Json.toString())
Sudhanshu Singh
08/09/2023, 1:00 PMcontent-type
but doesn't change Accept
defaultRequest {
url("baseurl")
// send request body as json
headers.appendIfNameAbsent(HttpHeaders.ContentType, ContentType.Application.Json.toString())
headers.appendIfNameAbsent(HttpHeaders.Accept, ContentType.Application.Json.toString())
}
client.get("endpoint") {
headers.appendIfNameAbsent(Accept, "test/csv")
headers.appendIfNameAbsent(HttpHeaders.ContentType, "text/csv")
}.body()
in the request headers i get both json and xml appended
accept: text/csv,application/json
content-type: test/csv
Rustam Siniukov
08/09/2023, 1:03 PMContentNegitiation
plugin? It will add Accept
header for all the serializers that you register with it.Sudhanshu Singh
08/09/2023, 1:04 PMinstall(ContentNegotiation) {
json(Json))
}
Rustam Siniukov
08/09/2023, 1:06 PMAccept
headers? It is not against the spec to do itSudhanshu Singh
08/09/2023, 1:07 PMRustam Siniukov
08/09/2023, 2:21 PMContentNegotiation
for some plugins. Should be an easy fix. Please create a feature request in YT. For now, you may need to create a separate client for this endpoint.