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

Feb 12, 2024 pm 03:03 PM
linux linux tutorial linux system linux command shell script pip command python package embeddedlinux Getting started with linux linux learning

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles