Hey! Is there a way to not set any headers with k...
# ktor
j
Hey! Is there a way to not set any headers with ktor client? I have the following code and it automatically sets the content-type header when run in the browser:
Copy code
val uploadHttpClient = HttpClient()
        val uploadResponse = uploadHttpClient.put(cloudflarePresignedPutUrl) {
            setBody(mediaFile)
        }
Sadly there's a bug in cloudflare's r2 that makes cors break when you set any header
a
You can do it by setting the
useDefaultTransformers
configuration property to
false
but, unfortunately, in this case you will lose an ability to receive response body as objects of standard types.
Copy code
val client = HttpClient {
    useDefaultTransformers = false
}
j
Thanks a lot! How would I set my bytearray as the body then? If I just pass it to setBody I get the following error:
Copy code
throwableExtensions.kt?010f:25 IllegalStateException: Fail to prepare request body for sending. 
The body type is: class String, with Content-Type: null.

If you expect serialized body, please check that you have installed the corresponding plugin(like `ContentNegotiation`) and set `Content-Type` header.
My code:
Copy code
val mediaFile = base64MediaFileString.decodeBase64Bytes()        
        val uploadHttpClient = HttpClient {
            useDefaultTransformers = false
        }
        val uploadResponse = uploadHttpClient.put(uploadLinkResponseBody.uploadUrl) {
            setBody(mediaFile)
        }
a
Copy code
val uploadResponse = uploadHttpClient.put(uploadLinkResponseBody.uploadUrl) {
    setBody(ByteArrayContent(mediaFile)))
}
❤️ 1
209 Views