If you often need to access a Web server via a terminal in non-interactive mode (for example, downloading files from a network, or testing a RESTful Network Service interface), you might choose a tool that is wget or curl. With a lot of command-line options, both of these tools can handle a lot of non-interactive network access (here, here, and here). However, even with powerful tools like these, you can only perform the functions you know about those options. Unless you're proficient with the cumbersome grammatical details, these tools are just simple Web downloads for you.
As it is advertised, "Give people a curl class tool," httpie designed to enhance wget and curl usability. Its primary goal is to make the process of interacting with a Web server through the command line as user-friendly as possible. For this reason, Httpie supports expressive, but very simple, intuitive syntax. It displays the response in color mode, and there are some good benefits, such as good support for JSON, and persistence sessions for workflow.
I know that a lot of people have doubts about replacing ubiquitous, usable, and perfect tools like wget and curl with completely unheard of software. This is a good idea, especially if you're a system administrator and have a lot of different hardware to deal with. However, for developers and end-users, it is important to be efficient. If I find a better alternative to the user of a tool, then I think it is no doubt to use an Easy-to-use version to save valuable time. There is no need to remain faithful to the tools that are replaced. After all, the best thing for Linux is to have a choice.
In this article, let's take a look at what I mean by Httpie, a user-friendly wget and curl alternative.
Install Httpie on Linux
Httpie is written in Python, so you can install it in almost every place (linux,macosx,windows). And, in most Linux distributions, there is a compiled installation package.
Debian,ubuntu or Linux Mint:
The code is as follows:
$ sudo apt-get install Httpie
Fedora:
The code is as follows:
$ sudo yum install Httpie
Centos/rhel:
First, enable the Epel warehouse, and then run:
The code is as follows:
$ sudo yum install Httpie
For any Linux distribution, the PIP is used for another installation method.
The code is as follows:
$ sudo pip install--upgrade httpie
Examples of Httpie
When you have finished installing Httpie, you can call it by entering the HTTP command. In the remainder of this article, I'll show you a few examples of useful HTTP commands.
Example 1: Customizing the head
You can use the format to customize the head. For example, we send an HTTP GET request to www.test.com, use a custom user agent (User-agent) and Source (Referer), and a custom header (such as Myparam).
The code is as follows:
$ http www.test.com user-agent:xmodulo/1.0 referer:http://xmodulo.com myparam:foo
Note that when you use the HTTP GET method, you do not need to explicitly specify the HTTP method.
This HTTP request appears as follows:
The code is as follows:
get/http/1.1
Host:www.jb51.net
Accept: */*
Referer:http://xmodulo.com
Accept-encoding:gzip, deflate, compress
Myparam:foo
user-agent:xmodulo/1.0
Example 2: Downloading files
You can use HTTP as a file downloader. You need to redirect the output to a file as follows.
The code is as follows:
$ http www.jb51.net/my_file.zip > My_file.zip
Or:
The code is as follows:
$ http--download www.jb51.net/my_file.zip
Example 3: Customizing the HTTP method
In addition to the default get method, you can also use other methods (such as Put,post,head). For example, send an HTTP put request:
The code is as follows:
$ http put www.jb51.net name= ' Dan Nanni ' email=dan@email.com
Example 4: Submitting a Form
Submitting a form using the HTTP command is easy, as follows:
The code is as follows:
$ http-f POST www.jb51.net name= ' Dan nanni ' comment= ' Hi there '
The '-f ' option causes the HTTP command to serialize the data field and set ' Content-type ' to ' application/x-www-form-urlencoded '; Charset=utf-8 ".
This HTTP POST request appears as follows:
The code is as follows:
post/http/1.1
Host:www.jb51.net
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 (an increasingly popular format for data interchange) support. In fact, the Httpie content type (content-type) used by default is JSON. Therefore, when you do not specify a content type to send data fields, they are automatically serialized as JSON objects.
The code is as follows:
$ http POST www.test.com name= ' Dan nanni ' comment= ' Hi there '
This HTTP POST request appears as follows:
The code is as follows:
post/http/1.1
Host:www.jb51.net
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, where you can use buffered data to provide HTTP request content. For example:
The code is as follows:
$ http POST Api.jb51.net/db/lookup < My_info.json
Or:
The code is as follows:
$ Echo ' {' name ': ' Dan nanni '} ' | HTTP POST api.test.com/db/lookup
Conclusion
In this article, I introduced Httpie, a possible alternative tool for wget and curl. In addition to the few simple examples shown here, you can find many interesting apps for Httpie on its official website. Once again, a powerful tool depends on how well you know it. Personally, I'm more inclined to Httpie because I'm looking for a simpler way to test a complex network interface.