dakrone / clj-http

An idiomatic clojure http client wrapping the apache client. Officially supported version.
http://clojars.org/clj-http
MIT License
1.78k stars 410 forks source link

URL encoding of whitespace should be "%20" instead of "+" in query parameters #626

Open viesti opened 1 year ago

viesti commented 1 year ago

Hi!

I ran into the following when talking to AWS SQS, when I sent data like

:query-params {"Action" "SendMessage"
               "MessageBody" "foo bar"}

which get's encoded into

:query-string "Action=SendMessage&MessageBody=foo+bar",

which the SQS service then rejects, because the whitespace in the foo bar should be encoded as %20 in the query parameter, instead of +.

I think that " " => "+" in query parameter is allowed by some services, since I didn't find an issue in this repository for this behavior, but strictly speaking, in URL encoding, the whitespace character should be encoded as %20.

clj-http uses java.net.URLEncoder and java.net.URLDecoder for both form and url encoding/decoding, but URLEncoder/URLDecoder documentation says:

Utility class for HTML form encoding.

Ring-Codec has utilities for both url and form encoding/decoding:

user> (ring.util.codec/url-encode " ")
"%20"
user> (ring.util.codec/form-encode " ")
"+"

So I guess ring-codec could be used.

viesti commented 1 year ago

Noting that a workaround, if one happens to run into this issue, is to append the query parameters to the URL, and ring.util.codec/url-encode can be used then for encoding.