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 tools under Linux

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article focuses on "how to use HTTPie tools under Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use HTTPie tools under Linux.

If you often need to access a web server in non-interactive mode through a terminal (for example, downloading files from the network, or testing the RESTful web service interface), the tool you may choose is wget or curl. With a large number of command-line options, both tools can handle many cases of non-interactive network access (such as here, here, and here). However, even with tools as powerful as these, you can only perform the functions of the options you know. Unless you are proficient in those cumbersome grammatical details, these tools are just simple web downloaders for you.

As it advertises, "give people curl-like tools," HTTPie is designed to enhance the usability of wget and curl. Its main goal is to make the process of interacting with the web server through the command line as humanized as possible. For this reason, HTTPie supports expressive but simple and intuitive syntax. It displays responses in color mode, and has some nice advantages, such as good support for JSON and persistent sessions for job streamlining.

I know that many people have doubts about replacing ubiquitous, available, and perfect tools like wget and curl with completely unheard of software. This view is good, especially if you are a system administrator and have to deal with a lot of different hardware. However, for developers and end users, efficiency is important. If I find a better alternative to the user of a tool, I think there is no doubt that an easy-to-use version will save valuable time. There is no need to be faithful to replaced tools. After all, the best thing for Linux is to have a choice.

In this article, let's learn about and show what I call HTTPie, a user-friendly alternative to wget and curl.

Install HTTPie on Linux

HTTPie is written in Python, so you can install it almost everywhere (Linux,MacOSX,Windows). Also, there is a compiled installation package in most Linux distributions.

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 repository, then run:

The code is as follows:

$sudo yum install httpie

For any Linux distribution, another installation method uses pip.

The code is as follows:

$sudo pip install-upgrade httpie

The example of HTTPie

When you have installed HTTPie, you can invoke it by typing the http command. In the rest of this article, I'll show you a few examples of useful http commands.

Example 1: custom head

You can use the format to customize the header. For example, we send a HTTP GET request to www.test.com, using a custom user agent (user-agent) and source (referer), as well as 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 using the HTTP GET method, there is no need to explicitly specify the HTTP method.

The HTTP request looks like this:

The code is as follows:

GET / HTTP/1.1

Host: www.yisu.com

Accept: * / *

Referer: http://xmodulo.com

Accept-Encoding: gzip, deflate, compress

MyParam: Foo

User-Agent: Xmodulo/1.0

Example 2: download files

You can use http as a file downloader. You need to redirect the output to the file as below.

The code is as follows:

$http www.yisu.com/my_file.zip > my_file.zip

Or:

The code is as follows:

$http-download www.yisu.com/my_file.zip

Example 3: customized HTTP method

In addition to the default GET method, you can use other methods (such as PUT,POST,HEAD). For example, send a HTTP PUT request:

The code is as follows:

$http PUT www.yisu.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 follows:

The code is as follows:

$http-f POST www.yisu.com 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".

The HTTP POST request looks like this:

The code is as follows:

POST / HTTP/1.1

Host: www.yisu.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 has built-in support for JSON, an increasingly popular data exchange format. 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 will be automatically serialized into JSON objects.

The code is as follows:

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

The HTTP POST request looks like this:

The code is as follows:

POST / HTTP/1.1

Host: www.yisu.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, 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

At this point, I believe you have a deeper understanding of "the use of HTTPie tools under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Servers

Wechat

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

12
Report