I have a client with `ContentNegotiation` / `json`...
# ktor
r
I have a client with
ContentNegotiation
/
json
installed. I want one particular request to return
application/pdf
. I've specified
accept(ContentType.Application.Pdf).withParameter("q", "2.0")
to try and get the server to return PDF instead, but it doesn't work -- seems the Spring server doesn't like a quality value > 1. It seems like the automatic
ContentNegotation
Accept
header should add
application/json
with a lower quality value so that the user can override it on a per-request basis. Is there a workaround to this?
o
Can't you use
Copy code
contentType(ContentType.Application.Pdf)
in the request? I use this for instance
Copy code
val response = <http://httpClient.post|httpClient.post>("...") {
    contentType(ContentType.Application.Json)
    setBody(obj)
}
r
ContentType
tells the server what the client is sending. I'm talking about the
Accept
header which tells the server what we want to receive in the response.
I created this KTOR issue / feature request, and also detailed a workaround in there that uses a custom plugin -- https://youtrack.jetbrains.com/issue/KTOR-7722/ContentNegotiation-client-plugin-no-way-to-opt-out-of-Accept-on-a-per-request-basis.
c
I guess the right approach would be to create and register a content negotiation plugin that can handle
application/pdf
. so when making the request, Ktor can handle the response and will set the header correctly if you specify it.
r
It won't solve the problem on its own. The new PDF registration for the
ContentNegotiation
plugin will simply result in both types added into the
Accept
header, which is exactly what already happens by specifying
accept(ContentType.Application.Pdf)
in the
RequestBuilder
.
c
did you try it or is that an assumption?
r
Its an assumption based on the code.
👍🏼 1