Hm.. Can not believe it is impossible to set custo...
# ktor
a
Hm.. Can not believe it is impossible to set custom headers for HTTP client 🙂 I try elaborate my issue. So, I play with this code:
Copy code
val response = client.call("some/url") {
        contentType(ContentType.Application.Json)
        method = <http://HttpMethod.Post|HttpMethod.Post>
        headers = HeadersBuilder ()
        body = data
    }
But compiler climes "Val can not be reassigned" against
headers
. So. how are you settings custom client headers?
📝 1
âś… 2
o
@e5l
e
Consider to use clear instead of reassign headers
a
@e5l Would you, please, be a little more elaborate? Say, I have got "Key1" to "Value1" and "Key2" to "Value2" pairs. How to add?
d
You can use the
header
function:
Copy code
val response = client.call(“some/url”) {
    contentType(ContentType.Application.Json)
    method = <http://HttpMethod.Post|HttpMethod.Post>
    headers.clear() // clear headers 
    header(“header”, “value”) // set one header
    headers { 
        header(“header”, “value”)
    }
    body = data
}
e
You could use
header
method, or add it by hand:
Copy code
client.call {
    headers.append("Hello", "World")
    headers.append("Hello2", "World2")
}
👍 1
d
headers is val, because you mutate it. You can call
headers.clear
, or
headers.append
. But if you don’t care about other headers, you can just call
header
Going to update the documentation with this
a
@Deactivated User @e5l Thanks, am going to try!
d
a
@Deactivated User O-o-o, excellent!!