scalaj-http
scalaj-http copied to clipboard
No way to pass query-string params when using postMulti?
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?
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.
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"
}
"