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

No way to pass query-string params when using postMulti?

Open sam opened this issue 8 years ago • 2 comments

The following curl command works fine against my API:

curl -vvvv -X PUT \
"http://localhost:9001/api/v1/events/one/mailers/jYCd?token=bob&rev=1173-eb559d86d2a58000aca5dc87760a1c9a" \
-F "[email protected]" \
-F "[email protected]"

Unfortunately I can't seem to build such a request with scalaj-http. When using .postMulti all .param calls (before and after the .postMulti; doesn't matter) add form fields instead of query-string parameters.

I've even tried "hard coding" the query-string into my URL:

protected[this] def request(path: String*): HttpRequest =
    Http(path.foldLeft(api.uri) { case (acc, segment) => acc / segment } toString())
      .param("token", api.token)

request("events", eventKey.id, "mailers", update.key.id, s"?token=bob&rev=${eventKey.rev}")
  .postMulti(
    MultiPart("document", "mailer.json", "application/json", write(update).getBytes("utf-8")),
    MultiPart("banner", "banner.jpg", mediaType.toString(), new FileInputStream(file), file.length(), _ => ()))
  .param("rev", eventKey.rev)
  .method("PUT")
  .asString

No luck. It strips the query-string from the URL.

Any way around this issue?

sam avatar Dec 06 '16 21:12 sam

For posterity, I got around it by adding .copy(urlBuilder = QueryStringUrlFunc) to my request after the .postMulti(...).

I'll leave if this open in case you think there's something nicer that could be done.

sam avatar Dec 06 '16 22:12 sam

Hey @sam , that seems like a decent workaround. Appending the params to the url should work too though. The Http library doesn't strip query string params, but maybe the URI library you're using does?

> Http("http://httpbin.org/post?foo=bar").postMulti(MultiPart("data","foo.txt","text/text","hello")).asString.body
res0: String = 
"{
  "args": {
    "foo": "bar"
  }, 
  "data": "", 
  "files": {
    "data": "hello"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", 
    "Accept-Encoding": "gzip,deflate", 
    "Cache-Control": "no-cache", 
    "Content-Length": "153", 
    "Content-Type": "multipart/form-data; boundary=--gc0pMUlT1B0uNdArYc0p", 
    "Host": "httpbin.org", 
    "Mime-Version": "1.0", 
    "Pragma": "no-cache", 
    "User-Agent": "scalaj-http/1.0"
  }, 
  "json": null, 
  "origin": "72.89.254.234", 
  "url": "http://httpbin.org/post?foo=bar"
}
"

hoffrocket avatar Mar 04 '17 23:03 hoffrocket