Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HTTPie

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use HTTPie. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

HTTPie is a command-line client for HTTP that aims to humanize the interaction between CLI and web services as much as possible. This tool provides concise http commands that allow arbitrary HTTP request data to be sent through natural syntax, showing colored output. HTTPie can be used for testing, debugging, and general interaction with HTTP servers.

Substitutes for Wget and cURL

You may have heard of the old Wget or slightly newer cURL tools that allow you to access Web from the command line. They are written to visit websites, while HTTPie is used to access Web API.

Web site requests occur between the computer and the end user who is reading and responding to what it sees, which is less dependent on a structured response. However, API requests make structured calls between two computers, and people are not part of the process, and the parameters of command-line tools such as HTTPie can effectively handle this.

Install HTTPie

There are several ways to install HTTPie. You can install it through the package manager, whether you are using brew, apt, yum, or dnf. However, if you have configured virtualenvwrapper, you can install it in your own way:

$mkvirtualenv httpie... (httpie) $pip install httpie... (httpie) $deactivate$ alias http=~/.virtualenvs/httpie/bin/http$ http-b GET https://httpbin.org/get{ "args": {}, "headers": {"Accept": "* / *", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org" "User-Agent": "HTTPie/1.0.2"}, "origin": "104.220.242.21010104.220.242.210", "url": "https://httpbin.org/get"}"

By pointing the http alias to a command in a virtual environment, you can run it even if the virtual environment is inactive. You can put the alias command in .bash _ profile or .bashrc so that you can upgrade HTTPie with the following command:

$~ / .virtualenvs/httpie/bin/pip install-U pip uses HTTPie to query the website

HTTPie simplifies queries and tests API. An option,-b (that is,-body) is used above. Without it, HTTPie will print the entire response by default, including the response header:

Http GET https://httpbin.org/getHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Connection: keep-aliveContent-Encoding: gzipContent-Length: 177Content-Type: application/jsonDate: Fri, 09 Aug 2019 20:19:47 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=block {"args": {}, "headers": {"Accept": "* / *", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.2"}, "origin": "104.220.242.210,104.220.242.210" "url": "https://httpbin.org/get"}"

This is important when debugging API services because a large amount of information is sent in the response header. For example, it is usually important to view the cookie sent. Httpbin.org provides a way to set up cookie (for testing purposes) through the URL path. The following sets a cookie with the title opensource and the value awesome:

Http GET https://httpbin.org/cookies/set/opensource/awesomeHTTP/1.1 302 FOUNDAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Connection: keep-aliveContent-Length: 223Content-Type: text/html; charset=utf-8Date: Fri, 09 Aug 2019 20:22:39 GMTLocation: / cookiesReferrer-Policy: no-referrer-when-downgradeServer: nginxSet-Cookie: opensource=awesome; Path=/X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=block "/ / W3C//DTD HTML 3.2 Final//EN" > Redirecting...Redirecting...You should be redirected automatically to target URL: "/ cookies" > / cookies. If not click the link.

Notice the response header of Set-Cookie: opensource=awesome; Path=/. This indicates that the cookie you expect to set has been set correctly, and the path is /. Also note that even if you get a 302 redirection, http will not follow it. If you want to follow the redirection, you need to explicitly use the-follow flag to request:

Http-- follow GET https://httpbin.org/cookies/set/opensource/awesomeHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Connection: keep-aliveContent-Encoding: gzipContent-Length: 66Content-Type: application/jsonDate: Sat, 10 Aug 2019 01:33:34 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=block {"cookies": {"opensource": "awesome"}}

But you can't see the original Set-Cookie header at this time. To see the intermediate response, you need to use-all:

Http-- headers-- all-- follow GET https://httpbin.org/cookies/set/opensource/awesomeHTTP/1.1 302 FOUNDAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Content-Type: text/html; charset=utf-8Date: Sat, 10 Aug 2019 01:38:40 GMTLocation: / cookiesReferrer-Policy: no-referrer-when-downgradeServer: nginxSet-Cookie: opensource=awesome; Path=/X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=blockContent-Length: 223Connection: keep-aliveHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Content-Encoding: gzipContent-Type: application/jsonDate: Sat, 10 Aug 2019 01:38:41 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockContent-Length: 66Connection: keep-alive

Printing the response body is not interesting because you only care about cookie most of the time. If you want to see the response header of the intermediate request instead of the response body in the final request, you can use:

$http-- print hb-- history-print h-- all-- follow GET https://httpbin.org/cookies/set/opensource/awesomeHTTP/1.1 302 FOUNDAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Content-Type: text/html; charset=utf-8Date: Sat, 10 Aug 2019 01:40:56 GMTLocation: / cookiesReferrer-Policy: no-referrer-when-downgradeServer: nginxSet-Cookie: opensource=awesome; Path=/X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=blockContent-Length: 223Connection: keep-aliveHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Content-Encoding: gzipContent-Type: application/jsonDate: Sat, 10 Aug 2019 01:40:56 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockContent-Length: 66Connection: keep-alive {"cookies": {"opensource": "awesome"}}

You can use-print to control exactly what is printed (h: response header; b: response body) and use-history-print to override the print settings requested in the middle.

Download binaries using HTTPie

Sometimes the response body is not in text form, but needs to be sent to a file that can be opened by different applications:

Http GET https://httpbin.org/image/jpegHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Connection: keep-aliveContent-Length: 35588Content-Type: image/jpegDate: Fri, 09 Aug 2019 20:25:49 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 Mode=block+--+ | NOTE: binary data not shown in terminal | +-- +

To get the correct picture, you need to save to a file:

Http-- download GET https://httpbin.org/image/jpegHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: * Connection: keep-aliveContent-Length: 35588Content-Type: image/jpegDate: Fri, 09 Aug 2019 20:28:13 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockDownloading 34.75 kB to "jpeg.jpe" Done. 34.75 kB in 0.00068s (50.05 MB/s)

Try it! The picture is lovely.

Send custom requests using HTTPie

You can send the specified request header. This is useful for custom Web API that requires non-standard headers:

$http GET https://httpbin.org/headers X-Open-Source-Com:Awesome {"headers": {"Accept": "* / *", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.2", "X-Open-Source-Com": "Awesome"}}

Finally, if you want to send a JSON field (although you can specify the exact content), for many less nested inputs, you can use shortcuts:

$http-body PUT https://httpbin.org/anything open-source=awesome author=moshez {"args": {}, "data": "{\" open-source\ ":\" awesome\ ",\" author\ ":\" moshez\ "}", "files": {}, "form": {}, "headers": {"Accept": "application/json, * / *", "Accept-Encoding": "gzip, deflate" "Content-Length": "46", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.2"}, "json": {"author": "moshez", "open-source": "awesome"}, "method": "PUT", "origin": "73.162.254.113, 73.162.254.113" "url": "https://httpbin.org/anything"}"

The next time you debug Web API, whether it's your own or someone else's, remember to put down cURL and try HTTPie as a command-line tool.

These are all the contents of the article "how to use HTTPie". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report