Is there any better alternative to getting tired of wget or curl?
If you often need to access the network server through a terminal in non-interactive mode (for example, download files from the network or test the RESTful Network Service Interface ), you may choose wget or curl. With a large number of command line options, both tools can handle a lot of non-interactive network access situations (such as here, here, and here ). However, even with powerful tools like these, you can only use the features of the options you know. Unless you are very familiar with the redundant syntax details, these tools are just a simple network download tool for you.
As advertised, HTTPie is designed to enhance the availability of wget and curl by using curl tools. Its main goal is to make the process of interacting with the network server through the command line as user-friendly as possible. Therefore, HTTPie supports expressive but simple and intuitive syntax. It displays responses in color mode, and has some good advantages, such as good support for JSON and persistent sessions for job flow.
I know many people have doubts about changing ubiquitous, available, and perfect tools like wget and curl into software that they have never heard. This is a good idea, especially if you are a system administrator and want to handle a lot of different hardware. However, efficiency is important for developers and end users. If I find a better alternative to a tool user, I think there is no doubt that using an easy-to-use version will save valuable time. There is no need to be loyal to the replaced tool. After all, for Linux, the best thing is to choose.
In this article, let's take a look at HTTPie, a user-friendly replacement of wget and curl.
Install HTTPie on Linux
HTTPie is written in Python, so you can install it in almost all places (Linux, MacOSX, Windows. In addition, most Linux distributions have compiled installation packages.
Debian, Ubuntu or Linux Mint:
$ sudo apt-get install httpie
Fedora:
$ sudo yum install httpie
CentOS/RHEL:
First, enable the EPEL repository and then run:
$ sudo yum install httpie
For any Linux release, use pip for another installation method.
$ sudo pip install --upgrade httpie
HTTPie example
After you install HTTPie, you can run the http command to call it. In the rest of this article, I will show several examples of useful http commands.
Example 1: Custom Headers
You can use the
$ http www.test.com User-Agent:Xmodulo/1.0Referer:http://xmodulo.com MyParam:Foo
Note that when the http get method is used, you do not need to specify the HTTP method explicitly.
The HTTP request looks as follows:
GET / HTTP/1.1
Host: www.test.com
Accept:*/*
Referer: http://xmodulo.com
Accept-Encoding: gzip, deflate, compress
MyParam: Foo
User-Agent: Xmodulo/1.0
Example 2: download an object
You can use http as a file download tool. You need to redirect the output to the file as follows.
$ http www.test.com/my_file.zip > my_file.zip
Or:
$ http --download www.test.com/my_file.zip
Example 3: Customize the HTTP Method
In addition to the default GET method, you can also use other methods (such as PUT, POST, and HEAD ). For example, to send an http put request:
$ http PUT www.test.com name='Dan Nanni' email=dan@email.com
Example 4: submit a form
It is easy to submit a form using the http Command, as shown below:
$ http -f POST www.test.com name='Dan Nanni' comment='Hi there'
The '-F' option enables the http command to serialize data fields and set 'content-type' to "application/x-www-form-urlencoded; charset = UTF-8 ".
The http post request looks as follows:
POST / HTTP/1.1
Host: www.test.com
Content-Length:31
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Accept-Encoding: gzip, deflate, compress
Accept:*/*
User-Agent: HTTPie/0.8.0
name=Dan+Nanni&comment=Hi+there
Example 5: JSON support
HTTPie built-in JSON (a popular data exchange format) support. In fact, the default content type (content-type) used by HTTPie is JSON. Therefore, when you do not specify the content type to send data fields, they are automatically serialized as JSON objects.
$ http POST www.test.com name='Dan Nanni' comment='Hi there'
The http post request looks as follows:
POST / HTTP/1.1
Host: www.test.com
Content-Length:44
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate, compress
Accept: application/json
User-Agent:HTTPie/0.8.0
{"name":"Dan Nanni","comment":"Hi there"}
Example 6: Output redirection
Another user-friendly feature of HTTPie is input redirection. You can use buffered data to provide HTTP request content. For example:
$ http POST api.test.com/db/lookup < my_info.json
Or:
$ echo '{"name": "Dan Nanni"}'| http POST api.test.com/db/lookup
Conclusion
In this article, I introduced HTTPie, a possible replacement tool for wget and curl. In addition to the simple examples shown here, you can find many interesting HTTPie applications on its official website. Repeat it again. A powerful tool also depends on your understanding of it. Personally, I prefer HTTPie, because I am looking for a more concise method to test complex network interfaces.
What do you think?
Via: http://xmodulo.com/wget-curl-alternative-linux.html
Author: Dan Nanni Translator: wangjiezhe Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
Source: http://linux.cn/article-5479-1.html
This article permanently updates the link address: