Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

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

Open
sam opened this issue Dec 6, 2016 · 2 comments
Open

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

sam opened this issue Dec 6, 2016 · 2 comments

Comments

@sam
Copy link

sam commented Dec 6, 2016

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 "document=@update-mailer.json" \
-F "banner=@sample.jpg"

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
Copy link
Author

sam commented Dec 6, 2016

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.

@hoffrocket
Copy link
Member

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"
}
"

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants