How to use Ubuntu Httpie

Source: Internet
Author: User
Tags linux mint

HTTPie is written in Python, so you can install it in almost all places (Linux,macosx,windows). Also, 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, use PIP for another installation method.

The code is as follows:

$ sudo pip install--upgrade httpie

Examples of HTTPie

When you are finished installing HTTPie, you can invoke 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


Format to customize the head. For example, we send an HTTP GET request to www.test.com, using 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 have to explicitly specify an HTTP method.

This HTTP request looks like this:

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 a file

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 protected]

Example 4: Submitting a Form

It is easy to submit the form using the HTTP command, 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 looks like this:

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 data interchange format, is supported. In fact, the content type (Content-type) that HTTPie uses 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 looks like this:

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 the input redirection, which you can use to provide HTTP request content using buffered data. 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

How to use Ubuntu Httpie

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.