scalaj-http icon indicating copy to clipboard operation
scalaj-http copied to clipboard

Header application order (and/or special case for User-Agent)

Open emanresusername opened this issue 8 years ago • 2 comments

I noticed that here the headers are applied in reverse order. This may well have further reaching implications, but it seems to do the wrong (or at least an unexpected) thing in the following scenario

import scalaj.http._
Http("http://user.agent").header("User-Agent", "i-expect-this-to-override-the-default")
// the User-Agent the server receives will still be the default (scalaj-http/1.0)

emanresusername avatar Nov 14 '16 03:11 emanresusername

Thanks. I vaguely remember that there was a reason I did that, but clearly the behavior is wrong. I'll investigate further and fix.

In the meantime, If you need a work around for setting the User-Agent you can create a new builder:

object MyHttp extends BaseHttp (
  proxyConfig: Option[Proxy] = None,
  options: Seq[HttpOptions.HttpOption] = HttpConstants.defaultOptions,
  charset: String = HttpConstants.utf8,
  sendBufferSize: Int = 4096,
  userAgent: String = "my-custom-user-agent",
  compress: Boolean = true
)

and then use MyHttp("http://user.agent")...

hoffrocket avatar Nov 14 '16 20:11 hoffrocket

Just a note that I encountered this myself too.

Ended up doing Http(url).copy(headers = List("User-Agent" -> "my-custom-user-agent")) instead

wjlow avatar Nov 29 '17 01:11 wjlow