[Translated]20150410 What is a good alternative to wget or curl on Linux.md

This commit is contained in:
wangjiezhe 2015-04-12 15:53:57 +08:00
parent 52ac56d12f
commit c2d0deed62
2 changed files with 145 additions and 145 deletions

View File

@ -1,145 +0,0 @@
wangjiezhe translating...
What is a good alternative to wget or curl on Linux
================================================================================
If you often need to access a web server non-interactively in a terminal environment (e.g., download a file from the web, or test REST-ful web service APIs), chances are that wget or curl is your go-to tool. With extensive command-line options, both of these tools can handle a variety of non-interactive web access use cases (examples [here][1], [here][2] and [here][3]). However, even powerful tools like these are only as good as your knowledge of how to use them. Unless you are well versed in the nitty and gritty details of their syntax, these tools are nothing more than simple web downloaders for you.
Billed as a "curl-like tool for humans," [HTTPie][4] is designed to improve on wget and curl in terms of usability. Its main goal is to make command-line interaction of a web server as human-friendly as possible. For that, HTTPie comes with expressive, yet very simple and intuitive syntax. It also displays responses in colorized formats for readability, and offers nice goodies like excellent JSON support and persistent sessions to streamline your workflows.
I know some of you will be skeptical about replacing a ubiquitously available, perfectly good tool such as wget or curl with totally unheard of software. This view has merit especially if you are a system admin who works with many different hardware boxes. For developers or end-users, however, I would say it's all about productivity. If I've found a user-friendly alternative of a tool, I don't see any problem adopting the easy to use version in my work environment to save my precious time. No need to be loyal and religious about what's being replaced. After all, choice is the best thing about Linux.
In this post, let me review HTTPie, and show you what I mean by HTTPie being a user-friendly alternative of wget and curl.
![](https://farm8.staticflickr.com/7633/16849137018_bcc7a616fc_b.jpg)
### Install HTTPie on Linux ###
HTTPie is written in Python, so you can install it pretty much everywhere (Linux, MacOSX, Windows). Even better, it comes as a prebuilt package on most Linux distributions.
#### Debian, Ubuntu or Linux Mint: ####
$ sudo apt-get install httpie
#### Fedora: ####
$ sudo yum install httpie
#### CentOS/RHEL: ####
First, enable [EPEL repository][5] and then run:
$ sudo yum install httpie
For any Linux distribution, an alternative installation method is to use [pip][6].
$ sudo pip install --upgrade httpie
### HTTPie Examples ###
Once you install HTTPie, you can invoke it by typing http command. In the rest of this article, let me show several useful examples of http command.
#### Example 1: Custom Headers ####
You can set custom headers in the format of <header:value>. For example, let's send an HTTP GET request to www.test.com, with custom user-agent and referer, as well as a custom header (e.g., MyParam).
$ http www.test.com User-Agent:Xmodulo/1.0 Referer:http://xmodulo.com MyParam:Foo
Note that when HTTP GET method is used, you don't need to specify any HTTP method.
The HTTP request will look like:
GET / HTTP/1.1
Host: www.test.com
Accept: */*
Referer: http://xmodulo.com
Accept-Encoding: gzip, deflate, compress
MyParam: Foo
User-Agent: Xmodulo/1.0
#### Example 2: Download a File ####
You can use http as a file downloader tool. You will need to redirect output to a file as follows.
$ http www.test.com/my_file.zip > my_file.zip
Alternatively:
$ http --download www.test.com/my_file.zip
#### Example 3: Custom HTTP Method ####
Besides the default GET method, you can use other methods (e.g., PUT, POST, HEAD). For example, to sent an HTTP PUT request:
$ http PUT www.test.com name='Dan Nanni' email=dan@email.com
#### Example 4: Submit a Form ####
Submitting a form with http command is as easy as:
$ http -f POST www.test.com name='Dan Nanni' comment='Hi there'
The '-f' option lets http command serialize data fields, and set 'Content-Type' to "application/x-www-form-urlencoded; charset=utf-8".
The HTTP POST request will look like:
POST / HTTP/1.1
Host: www.test.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 comes with built-in JSON support, which is nice considering its growing popularity as a data exchange format. In fact, the default content-type used by HTTPie is JSON. So if you send data fields without specifying any content-type, they will automatically be serialized as a JSON object.
$ http POST www.test.com name='Dan Nanni' comment='Hi there'
The HTTP POST request will look like:
POST / HTTP/1.1
Host: www.test.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: Input Redirect ####
Another nice user-friendly feature of HTTPie is input redirection, where you can feed an HTTP request body with buffered data. For example, you can do things like:
$ http POST api.test.com/db/lookup < my_info.json
or:
$ echo '{"name": "Dan Nanni"}' | http POST api.test.com/db/lookup
### Conclusion ###
In this article, I introduce to you HTTPie, a possible alternative to wget or curl. Besides these simple examples presented here, you can find a lot of interesting use cases of HTTPie at the [official site][7]. Again, a powerful tool is only as good as your knowledge about the tool. Personally I am sold on HTTPie, as I was looking for a way to test complicated web APIs more easily.
What's your thought?
--------------------------------------------------------------------------------
via: http://xmodulo.com/wget-curl-alternative-linux.html
作者:[Dan Nanni][a]
译者:[wangjiezhe](https://github.com/wangjiezhe)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://xmodulo.com/author/nanni
[1]:http://xmodulo.com/how-to-download-multiple-files-with-wget.html
[2]:http://xmodulo.com/how-to-use-custom-http-headers-with-wget.html
[3]:http://ask.xmodulo.com/custom-http-header-curl.html
[4]:https://github.com/jakubroztocil/httpie
[5]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[6]:http://ask.xmodulo.com/install-pip-linux.html
[7]:https://github.com/jakubroztocil/httpie

View File

@ -0,0 +1,145 @@
Linux 上 wget 或 curl 的更佳替代品
================================================================================
如果你经常需要通过终端以非交互模式访问网络服务器(例如,从网络上下载文件,或者是测试 RESTful 网络服务接口),可能你会选择的工具是 wget 或 curl。通过大量的命令行选项这两种工具都可以处理很多非交互网络访问的情况比如[这里][1][这里][2],还有[这里][3])。然而,即使像这些一样的强大的工具,也只是与你对如何使用它们的了解程度等同。除非你很精通那些又多又笨(原文是 nitty and gritty的语法细节这些工具对于你来说只不过是简单的网络下载器。
就像宣传的那样,“为人类着想的类 curl 工具”,[HTTPie][4] 设计用来增强 wget 和 curl 的可用性。它的主要目标是使通过命令行与网络服务器进行交互的过程变得尽可能的人性化。为此HTTPie 支持具有表现力,但又很简单很直观的语法。它以彩色模式显示响应,并且还有一些不错的优点,比如对 JSON 的良好支持,和持久性会话用以作业流程化。
我知道很多人对把像 wget 和 curl 这样的无处不在的可用的完美工具换成完全没听说过的软件心存怀疑。这种观点是好的,特别是如果你是一个系统管理员、要处理很多不同的硬件的话。然而,对于开发者和终端用户来说,重要的是效率。如果我发现了一个工具的用户友好替代,我没有看到任何问题如果你采用易于使用的版本来节省你宝贵的时间。没有必要对替换掉的工具保持信仰忠诚。毕竟,对于 Linux 来说,最好的事情是可以选择。
在这篇文章中,让我们来回顾并展示一下我所说的 HTTPie一个用户友好的 wget 和 curl 的替代。
![](https://farm8.staticflickr.com/7633/16849137018_bcc7a616fc_b.jpg)
### 在 Linux 上安装 HTTPie ###
HTTPie 是用 Python 写的所以你可以在几乎所有地方LinuxMacOSXWindows安装它。而且在大多数的 Linux 发行版中都有编译好的安装包。
#### DebianUbuntu 或者 Linux Mint ####
$ sudo apt-get install httpie
#### Fedora ####
$ sudo yum install httpie
#### CentOS/RHEL ####
首先,启用[EPEL 仓库][5],然后运行:
$ sudo yum install httpie
对于任何 Linux 发行版,另一个安装方法时使用[pip][6]。
$ sudo pip install --upgrade httpie
### HTTPie 的例子 ###
当你安装完 HTTPie 后,你可以通过输入 http 命令来调用它。在这篇文章的剩余部分,我会展示几个有用的 http 命令的例子。
#### 例1定制头部 ####
你可以使用 <header:value> 的格式来定制头部。例如,我们发送一个 HTTP GET 请求到 www.test.com使用定制用户代理user-agent和来源referer还有定制头部比如 MyParam
$ http www.test.com User-Agent:Xmodulo/1.0 Referer:http://xmodulo.com MyParam:Foo
注意到当使用 HTTP GET 方法时,你无需指定任何 HTTP 方法。
这个 HTTP 请求看起来如下:
GET / HTTP/1.1
Host: www.test.com
Accept: */*
Referer: http://xmodulo.com
Accept-Encoding: gzip, deflate, compress
MyParam: Foo
User-Agent: Xmodulo/1.0
#### 例2下载文件 ####
你可以把 http 作为文件下载器来使用。你需要像下面一样把输出重定向到文件。
$ http www.test.com/my_file.zip > my_file.zip
或者:
$ http --download www.test.com/my_file.zip
#### 例3定制 HTTP 方法 ####
除了默认的 GET 方法,你还可以使用其他方法(比如 PUTPOSTHEAD。例如发送一个 HTTP PUT 请求:
$ http PUT www.test.com name='Dan Nanni' email=dan@email.com
#### 例4提交表单 ####
使用 http 命令提交表单很容易,如下:
$ http -f POST www.test.com name='Dan Nanni' comment='Hi there'
'-f' 选项使 http 命令序列化数据字段,并将 'Content-Type' 设置为 "application/x-www-form-urlencoded; charset=utf-8"。
这个 HTTP POST 请求看起来如下:
POST / HTTP/1.1
Host: www.test.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
####例JSON 支持
HTTPie 内置 JSON一种日渐普及的数据交换格式支持。事实上HTTPie 默认使用的内容类型content-type就是 JSON。因此当你不指定内容类型发送数据字段时它们会自动序列化为 JSON 对象。
$ http POST www.test.com name='Dan Nanni' comment='Hi there'
这个 HTTP POST 请求看起来如下:
POST / HTTP/1.1
Host: www.test.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"}
#### 例6输出重定向 ####
HTTPie 的另外一个用户友好特性是输入重定向,你可以使用缓冲数据提供 HTTP 请求内容。例如:
$ http POST api.test.com/db/lookup < my_info.json
或者:
$ echo '{"name": "Dan Nanni"}' | http POST api.test.com/db/lookup
### 结束语 ###
在这篇文章中,我介绍了 HTTPie一个 wget 和 curl 的可能替代工具。除了这里展示的几个简单的例子,你可以在[官方网站][7]上找到 HTTPie 的很多有趣的应用。再次重复一遍,一款强大的工具也只相当于你对它的了解程度。从个人而言,我更热衷于 HTTPie因为我在寻找一种更简洁的测试复杂网络接口的方法。
你怎么看?
--------------------------------------------------------------------------------
via: http://xmodulo.com/wget-curl-alternative-linux.html
作者:[Dan Nanni][a]
译者:[wangjiezhe](https://github.com/wangjiezhe)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://xmodulo.com/author/nanni
[1]:http://xmodulo.com/how-to-download-multiple-files-with-wget.html
[2]:http://xmodulo.com/how-to-use-custom-http-headers-with-wget.html
[3]:http://ask.xmodulo.com/custom-http-header-curl.html
[4]:https://github.com/jakubroztocil/httpie
[5]:http://xmodulo.com/how-to-set-up-epel-repository-on-centos.html
[6]:http://ask.xmodulo.com/install-pip-linux.html
[7]:https://github.com/jakubroztocil/httpie