If you work with a REST API, you may use curl to explore the various actions. The Python project HTTPie is a user-friendly replacement with a much clearer syntax.
This post is part of my journey to learn Python. You can find the other parts of this series here.
Installing HTTPie
HTTPie is an HTTP client you can use to explore (web) APIs. It comes with a great documentation, supports JSON, offers syntax highlighting and understands sessions. It works with everything that supports HTTP and not just applications written in Python.
You can install HTTPie using pip. It is such a great help that you may install HTTPie into your global Python installation and not just into a virtual environment:
1 |
pip install httpie |
http or https?
HTTPie comes with to executables: http and https. The http command uses the default scheme HTTP, while https uses HTTPS. Depending on what you need more, using the right executable can save you a lot of typing.
Common use-cases
You can fetch the HTML of a web site by entering the URL of that site:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
PS D:\> https jgraber.ch HTTP/1.1 200 OK Connection: keep-alive Content-Encoding: gzip Content-Security-Policy: default-src 'self'; script-src 'self' 'sha256-DpoBpZiqPfTSgCe3TsXOHkvf/Z25WDRWfNz+kaz2g44=' www.google-analytics.com www.googletagmanager.com; img-src 'self' www.google-analytics.com Content-Type: text/html; charset=UTF-8 Date: Sun, 26 Jul 2020 20:53:06 GMT Feature-Policy: geolocation 'self' Referrer-Policy: no-referrer Server: nginx Strict-Transport-Security: max-age=31536000 Transfer-Encoding: chunked Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: DENY X-Powered-By: PleskLin X-XSS-Protection: 1; mode=block <!DOCTYPE html> <html lang="en"> … |
To get only the headers of your request and the response, you can use the option –print=Hh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
PS D:\> https --print=Hh jgraber.ch GET / HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Connection: keep-alive Host: jgraber.ch User-Agent: HTTPie/2.2.0 HTTP/1.1 200 OK Connection: keep-alive Content-Encoding: gzip Content-Security-Policy: default-src 'self'; script-src 'self' 'sha256-DpoBpZiqPfTSgCe3TsXOHkvf/Z25WDRWfNz+kaz2g44=' www.google-analytics.com www.googletagmanager.com; img-src 'self' www.google-analytics.com Content-Type: text/html; charset=UTF-8 Date: Sun, 26 Jul 2020 18:17:17 GMT Feature-Policy: geolocation 'self' Referrer-Policy: no-referrer Server: nginx Strict-Transport-Security: max-age=31536000 Transfer-Encoding: chunked Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: DENY X-Powered-By: PleskLin X-XSS-Protection: 1; mode=block |
You can send cookies as a string with the format 'Cookie:key=value'
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
PS D:\> https httpbin.org/cookies 'Cookie:sessionid=foo;another-cookie=bar' HTTP/1.1 200 OK Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: * Connection: keep-alive Content-Length: 76 Content-Type: application/json Date: Sun, 26 Jul 2020 18:18:48 GMT Server: gunicorn/19.9.0 { "cookies": { "another-cookie": "bar", "sessionid": "foo" } } |
You can check if a specific SSL/TLS version is supported on your web server (if not, you get something like this output:
1 2 |
PS D:\> https --ssl='tls1.1' https://srv.local http: error: ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer')) while doing GET request to URL: https://srv.local/ |
Those examples are just the tip of the iceberg. There are many more options you can use to get from HTTPie exactly the request you want. The more you use this tool, the more you are going to like it.
Conclusion
I find HTTPie a great addition to my tool list. It offers many options to explore an HTTP API and the syntax highlighting ensures that you spot the important parts.
3 thoughts on “Python Friday #31: HTTPie as a Curl Replacement”