Is there any better alternative to getting tired of wget or curl?

Source: Internet
Author: User
Tags linux mint

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:
  1. $ sudo apt-get install httpie

 

Fedora:
  1. $ sudo yum install httpie

 

CentOS/RHEL:

First, enable the EPEL repository and then run:

  1. $ sudo yum install httpie

For any Linux release, use pip for another installation method.

  1. $ 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

  1. $ 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:

  1. GET / HTTP/1.1
  2. Host: www.test.com
  3. Accept:*/*
  4. Referer: http://xmodulo.com
  5. Accept-Encoding: gzip, deflate, compress
  6. MyParam: Foo
  7. 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.

  1. $ http www.test.com/my_file.zip > my_file.zip

Or:

  1. $ 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:

  1. $ 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:

  1. $ 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:

  1. POST / HTTP/1.1
  2. Host: www.test.com
  3. Content-Length:31
  4. Content-Type: application/x-www-form-urlencoded; charset=utf-8
  5. Accept-Encoding: gzip, deflate, compress
  6. Accept:*/*
  7. User-Agent: HTTPie/0.8.0
  8. 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.

  1. $ http POST www.test.com name='Dan Nanni' comment='Hi there'

The http post request looks as follows:

  1. POST / HTTP/1.1
  2. Host: www.test.com
  3. Content-Length:44
  4. Content-Type: application/json; charset=utf-8
  5. Accept-Encoding: gzip, deflate, compress
  6. Accept: application/json
  7. User-Agent:HTTPie/0.8.0
  8. {"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:

  1. $ http POST api.test.com/db/lookup < my_info.json

Or:

  1. $ 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.