Fuel: Headers not sticking

Created on 5 Feb 2016  路  7Comments  路  Source: kittinunf/fuel

I don't know if I'm doing something wrong here but I can't seem to get my header changes to stick.

Here's what I'm doing:

Manager.instance.baseHeaders = mapOf(Pair("Content-Type", "application/json"))
val (request, response, data) = url.httpPost(listOf(Pair("emailAddress", email))).responseString()

However, when I debug the Manager class' request method (around line 57) the request's headers are set to:

"Content-Type" -> "application/x-www-form-urlencoded"
"Accept-Encoding" -> "compress;q=0.5, gzip;q=1.0"

This is using just Fuel (not the android lib) in a Spring Boot app with the Kotlin RC

bug

Most helpful comment

Oh I am sorry. What I actually mean is that you can override httpHeader it with header function at request level. url.header("Content-Type" to "application/json").httpPost ....

All 7 comments

I dug a little deeper and it looks like the Encoding class is overriding my headers. Looking at this class it seems that ultimately multipart/form-data and application/x-www-form-urlencoded are the only two Content-Type values that end up being used. Is this the desired behavior?

I put those 2 as the sane defaults. I anticipated that it would cover most of the case. However, It is true that those 2 values should be configurable. I am sorry for that. I will make some improvements in next coming release.

In the meantime, can you override this at the request level? Meaning that, you could do

url.httpPost(listOf("Content-Type" to "application/json", "emailAddress" to email).responseString() in order to override the defaults

@kittinunf I tried that override but Content-Type needs to be a header not a request parameter, so it didn't work.

Oh I am sorry. What I actually mean is that you can override httpHeader it with header function at request level. url.header("Content-Type" to "application/json").httpPost ....

@erikthered I addressed this issue in PR #51. Thanks for reporting this.

I stole your use case to become a test case :yellow_heart:

https://github.com/kittinunf/Fuel/blob/fix-bugs-and-cleanup/fuel/src/test/kotlin/com/github/kittinunf/fuel/BlockingRequestTest.kt#L198-L213

Cool! One other suggestion I would make is to add the request level header function to the readme. I didn't even realize that function was available.

Agree. I'll do it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lfmingo picture lfmingo  路  4Comments

iNoles picture iNoles  路  5Comments

liamkernighan picture liamkernighan  路  5Comments

iNoles picture iNoles  路  5Comments

jsyeo picture jsyeo  路  5Comments