Home > System Tutorial > LINUX > How to install and use HTTPie and HTTP Prompt on Linux

How to install and use HTTPie and HTTP Prompt on Linux

WBOY
Release: 2024-02-12 15:03:23
forward
680 people have browsed it

HTTPie is a command line HTTP client built for modern web APIs. It provides intuitive commands and user-friendly interface. In this guide, you'll learn about HTTPie's features and how it compares to cURL. You will also learn how to install and start using HTTPie on your Linux system.

Before you begin

If you have not done so already, please create an account.

Follow our guide to setting up and securing a compute instance to update your system. You may also want to set the time zone, configure the hostname, create a limited user account, and enforce SSH access.

Please note

The steps in this guide are written for non-root users. Commands that require elevated privileges are prefixed with sudo. If you are unfamiliar with the sudo command, see https://www.linuxmi.com/linux-sudo-command.html.

What is HTTPie?

HTTPie is a command-line HTTP client similar to cURL. But unlike cURL, HTTPie is designed to be easier to use with modern web APIs. HTTPie's syntax is web services oriented. Its interface is more readable and user-friendly than cURL. These features make HTTPie a great tool for testing, debugging, or otherwise consuming web services from the command line.

HTTPie vs cURL

This section will explore why you might choose to use HTTPie instead of cURL, especially since cURL is installed by default on many Linux distributions. cURL excels at extending options to meet a wide range of HTTP needs. HTTPie focuses on supporting queries to modern web APIs. When using web APIs, it provides the most relevant details and hides information you are unlikely to need. HTTPie's output allows you to interact with web services more intuitively and clearly.

If you want to use web APIs, especially RESTful APIs that use JSON data, you should consider using HTTPie. Alternatively, if you want an HTTP client for more general needs, consider using cURL, as it provides some options to make it more adaptable.

You can learn more about curlie from our How to Install and Use Curlie Command on Linux, which is a modern command-line HTTP client with the readability of HTTPie and the adaptability of cURL .

How to install HTTPie

HTTPie is available from the package managers of most major Linux distributions. Below are the commands you can use when installing HTTPie through the package managers of different distributions.

On Debian and Ubuntu, use the following command:

sudo apt install httpie

Under AlmaLinux and CentOS operating systems, execute the following command:

sudo yum install httpie

On Fedora, use the following command:

sudo dnf install httpie

After installing HTTPie, you can verify the installation and access it using the http command.

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ http --version
1.0.3
Copy after login

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

How to use HTTPie

If you are familiar with other HTTP clients, httppie should be easier to learn. Most of its basic usage is similar to cURL, but it allows you to simplify the commands you need.

The following sections will introduce how to use HTTPie to handle the most common HTTP requests when using web services.

Basic usage

You can see the most basic usage of HTTPie in the GET request. Like cURL, HTTPie does not require you to specify the request method. Just provide the command and request URL.

HTTPie's output includes response header information by default. It uses syntax highlighting to make responses easier to read, as you can see in the screenshot below:

Adding header data to a request in httppie requires appending the data to the URL, as shown in the command below. The following example fetches a random "dad joke" from a web service. You can see that the command also adds the --follow option, which makes HTTPie follow any URL redirects (equivalent to -L in curl). HTTPie includes redirect response header information in the output.

Request method

To specify a request method in httppie, simply include the method name—GET, POST, PUT, DELETE, etc.—as the first part of the http command. This can be seen in the next example, which uses HTTPie's own web service for testing.

# Display request information (including return header 200)
http www.linuxmi.com

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

# Display detailed request (including request and return header 200)
http -v www.linuxmi.com

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

# Show only Header
http -h www.linuxmi.com
http –head www.linuxmi.com
http –header www.linuxmi.com
http –headers www.linuxmi.com

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

# 只显示Body
http -b www.linuxmi.com
http –body www.linuxmi.com

# 下载文件
http -d www.linuxmi.com

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

# 模拟提交表单
http -f POST www.linuxmi.com username=’linuxmi-user’

# 请求删除的方法
http DELETE www.linuxmi.com

# 传递JSON数据请求(默认就是JSON数据请求)
http PUT www.linuxmi.com username=’linuxmi-user’ password=’linuxmi-pwd’

# 如果JSON数据存在不是字符串则用:=分隔,例如
http PUT www.linuxmi.com username=’linuxmi-user’ password=’linuxmi-pwd’ age:=28 a:=true streets:='[“a”, “b”]’

# 模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8
http –form POST www.linuxmi.com username=’linuxmi-user’

# 模拟Form的上传, Content-Type: multipart/form-data
http -f POST www.linuxmi.com/jobs username=’linuxmi-user’ file@~/test.pdf

# 修改请求头, 使用:分隔
http www.linuxmi.com User-Agent:mimvp-agent/1.0 ‘Cookie:a=b;b=c’ Referer:http://www.linuxmi.com/

# 认证
http -a username:password www.linuxmi.com
http –auth-type=digest -a username:password www.linuxmi.com

HTTP Prompt – 交互式命令行HTTP客户端

HTTP Prompt (或HTTP-prompt) 是基于HTTPie和prompt_toolkit构建的交互式命令行HTTP客户端,具有自动完成和语法突出显示功能。 它还支持自动cookie,OpenAPI/Swagger集成以及类Unix管道和输出重定向。 此外,它还提供了20多个可以使用的主题。

我们现在将解释如何在 Linux 中安装和简要使用 HTTP-prompt。

如何在Linux中安装HTTP Prompt

您可以使用PIP命令安装HTTP提示,就像常规Python包一样,如图所示。

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ pip install http-prompt
Copy after login
如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用
pip install Pygments==2.5.2
Copy after login

如果您尝试在系统范围的Python上安装HTTP-prompt,则可能会收到一些权限错误。 不建议这样做,但如果这是您想要做的,只需使用sudo命令获得root权限。

或者,您可以使用–user选项将软件包安装到用户主目录中,如下所示:

pip install --user http-prompt
Copy after login

要升级HTTP提示符,请执行以下操作:

pip install -U http-prompt
Copy after login

如何在Linux中使用HTTP Prompt

要启动会话,只需运行http-prompt命令,如图所示。

从最后一个会话开始或http://localhost:8000

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ http-prompt 
Version: 2.1.0
http://localhost:8000> httpie post
http POST http://localhost:8000
http://localhost:8000> 
Copy after login
如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

从给定的URL开始

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ http-prompt http://localhost:3000
Copy after login
如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

从一些初始选项开始

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ http-prompt localhost:300/api --auth user:linuxmi username=linuxmi
Copy after login
如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

要预览HTTP Prompt将如何调用HTTPie,请运行以下命令。

http://localhost:300/api> httpie post
http –auth=user:linuxmi POST http://localhost:300/api username=linuxmi

启动会话后,您可以交互式键入命令

您可以发送HTTP请求,输入一个HTTP方法,如下所示。

> head

如何在 Linux 上 HTTPie 与 HTTP Prompt 安装和使用

> get
> post
> put
> patch
> delete

可以添加标头,查询字符串或正文参数,使用HTTPie中的语法。 这里有些例子:

# 设置 header
> Content-Type:application/json

# 查询字符串参数
> page==5

# body 参数
> username=linuxmi
> full_name=’www.linuxmi.com’

# 原始JSON中的body参数
> number:=18719
> is_ok:=true
> names:=[“linuxmi”,”com”] > user:='{“username”: “linuxmi”, “password”: “linuxmi”}’

# 把所有东西都写成一行
> Content-Type:application/json page==5 username=linuxmi

您还可以添加HTTPie选项,如图所示。

> –form –auth user:pass
> –verify=no
或者
> –form –auth user:pass username=linuxidc Content-Type:application/json

要重置会话(清除所有参数和选项)或退出会话,请运行:

> rm * #重置会话
> exit #退出会话

有关更多信息和用法示例,请参阅HTTP-prompt文档:http://http-prompt.com/。

结论

OK, that’s it! HTTP Prompt is the perfect companion to HTTPie.

Now you should be able to start sending requests to the web api using HTTPie. If you find yourself looking for more advanced features, you can find them when digging deeper into HTTPie. Check out httppie's official documentation and refer to the http --help command to start learning more about httppie's features.

We would love to hear from you. Share your thoughts or ask questions about HTTP Prompt vs. HTTPie in the comments below.

The above is the detailed content of How to install and use HTTPie and HTTP Prompt on Linux. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template