httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.37k stars 3.67k forks source link

URL Encoding quoted strings #309

Closed SharaaEsper closed 9 years ago

SharaaEsper commented 9 years ago

Hi, httpie already encodes spaces with %20 when the string is quoted but does not URL encode other characters.

For instance

http GET "http://localhost:9200/_search?q=+@fields.program:nova AND compute NOT api +@message:557d73222e184440b85661a5a7746217” -v

is encoded as

GET /_search?q=+@fields.program:nova%20AND%20compute%20NOT%20api%20+@message:557d73222e184440b85661a5a7746217

This causes issues when trying to have additional search params in ES as it neglects the things after the +.

The proper encoding should be:

GET /_search?q=%2B%40fields.program%3Anova%20AND%20compute%20NOT%20api%20%2B%40message%3A557d73222e184440b85661a5a7746217

Would it be possible to have an option you can pass to urlencode (--url-encode or something?), or to simply do it if the string is quoted (There aren't many times you would want to send un-encoded characters in your query string, but I imagine there are edge cases)

sigmavirus24 commented 9 years ago

Actually + is perfectly valid without being %-encoded. In fact, + is often used instead of %20 to hold the place of spaces.

Would it be possible to have an option you can pass to urlencode (--url-encode or something?), or to simply do it if the string is quoted

That is theoretically possible but I strongly doubt its usefulness. There isn't anything equivalent in curl as far as I remember.

There aren't many times you would want to send un-encoded characters in your query string, but I imagine there are edge cases

Define "un-encoded characters" because as far as RFC language is concerned, you're saying that every character in the query string should be %-encoded, which I don't think you meant. @ is also perfectly valid un-encoded in query strings.

SharaaEsper commented 9 years ago

That's my mistake (Too much fighting with ES I guess), and this problem is specific to when the application is expecting the string to be completely urlencoded(So pretty much everything but "." encoded)The behavior I'm hoping for would be the --data-urlencode functionality in cURL, just preferably cleaner.

Example:

curl -s -G "http://localhost:9200/_search" --data-urlencode "q=+@fields.program:nova AND compute NOT api +@message:0fa01985-9c1e-4552-8f62-948086b5ed6c"

Results in the

> GET /_search?q=%2B%40fields.program%3Anova%20AND%20compute%20NOT%20api%20%2B%40message%3A0fa01985-9c1e-4552-8f62-948086b5ed6c HTTP/1.1

That I needed. I'd like an equivalent option in httpie

jkbrzt commented 9 years ago

@Ryuujinx You can use the param==value notation to get full encoding of the values for URL parameters.

Say you want to GET this URL:

http://httpbin.org/get?q=%2B%40fields.program%3Anova+AND+compute+NOT+api+%2B%40message%3A557d73222e184440b85661a5a7746217

$ http -v httpbin.org/get q=='+@fields.program:nova AND compute NOT api +@message:557d73222e184440b85661a5a7746217'
GET /get?q=%2B%40fields.program%3Anova+AND+compute+NOT+api+%2B%40message%3A557d73222e184440b85661a5a7746217 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: httpbin.org
User-Agent: HTTPie/0.9.1
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 427
Content-Type: application/json
Date: Thu, 12 Feb 2015 14:08:07 GMT
Server: nginx

{
    "args": {
        "q": "+@fields.program:nova AND compute NOT api +@message:557d73222e184440b85661a5a7746217"
    },
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/0.9.1"
    },
    "origin": "109.81.210.175",
    "url": "http://httpbin.org/get?q=%2B%40fields.program%3Anova+AND+compute+NOT+api+%2B%40message%3A557d73222e184440b85661a5a7746217"
}

Does that do the trick for you?

SharaaEsper commented 9 years ago

@jakubroztocil

That works perfectly, thanks.