21 个 curl 命令练习
curl
![](/data/attachment/album/201912/05/210334mg36oidhkb585hg5.jpg)


最近，我对人们如何学习新事物感兴趣。我正在读 Kathy Sierra 的好书《[Badass: Making Users Awesome](https://www.amazon.com/Badass-Making-Awesome-Kathy-Sierra/dp/1491919019)》，它探讨了有关“刻意练习”的想法。这个想法是，你找到一个可以用三节 45 分钟课程内能够学会的小技能，并专注于学习这项小技能。因此，作为一项练习，我尝试考虑一项能够在三节 45 分钟课程内学会的计算机技能。


我认为使用 `curl` 构造 HTTP 请求也许就是这样的一项技能，所以这里有一些 `curl` 练习作为实验！


### 什么是 curl ?


`curl` 是用于构造 HTTP 请求的命令行工具。我喜欢使用 `curl`，因为它能够很轻松地测试服务器或 API 的行为是否符合预期，但是刚开始接触它的时候会让你感到一些困惑！


下面是一幅解释 `curl` 常用命令行参数的漫画 （在我的 [Bite Size Networking](https://wizardzines.com/zines/bite-size-networking) 杂志的第 6 页）。


![](/data/attachment/album/201912/05/210411aqpo59rka1kt51gk.jpg)


### 熟能生巧


对于任何命令行工具，我认为熟练使用是很有帮助的，能够做到只输入必要的命令真是太好了。例如，最近我在测试 Gumroad API，我只需要输入：



```
curl https://api.gumroad.com/v2/sales \
                         -d "access_token=<SECRET>" \
                         -X GET  -d "before=2016-09-03"
```

就能从命令行中得到想要的结果。


### 21 个 curl 练习


这些练习是用来理解如何使用 `curl` 构造不同种类的 HTTP 请求的，它们是故意有点重复的，基本上包含了我需要 `curl` 做的任何事情。


为了简单起见，我们将对 <https://httpbin.org> 发起一系列 HTTP 请求，httpbin 接受 HTTP 请求，然后在响应中回显你所发起的 HTTP 请求。


1. 请求 <https://httpbin.org>
2. 请求 <https://httpbin.org/anything>，它将会解析你发起的请求，并且在响应中回显。`curl` 默认发起的是 GET 请求
3. 向 <https://httpbin.org/anything> 发起 GET 请求
4. 向 <https://httpbin.org/anything> 发起 GET 请求，但是这次需要添加一些查询参数（设置 `value=panda`）
5. 请求 Google 的 `robots.txt` 文件 ([www.google.com/robots.txt](http://www.google.com/robots.txt))
6. 向 <https://httpbin.org/anything> 发起 GET 请求，并且设置请求头为 `User-Agent: elephant`
7. 向 <https://httpbin.org/anything> 发起 DELETE 请求
8. 请求 <https://httpbin.org/anything> 并获取响应头信息
9. 向 <https://httpbin.com/anything> 发起请求体为 JSON `{"value": "panda"}` 的 POST 请求
10. 发起与上一次相同的 POST 请求，但是这次要把请求头中的 `Content-Type` 字段设置成 `application/json`（因为 POST 请求需要一个与请求体相匹配的 `Content-Type` 请求头字段）。查看响应体中的 `json` 字段，对比上一次得到的响应体
11. 向 <https://httpbin.org/anything> 发起 GET 请求，并且在请求头中设置 `Accept-Encoding: gzip`（将会发生什么？为什么会这样？）
12. 将一些 JSON 放在文件中，然后向 <https://httpbin.org/anything> 发起请求体为该文件的 POST 请求
13. 设置请求头为 `Accept: image/png` 并且向 <https://httpbin.org/image> 发起请求，将输出保存为 PNG 文件，然后使用图片浏览器打开。尝试使用不同的 `Accept:` 字段去请求此 URL
14. 向 <https://httpbin.org/anything> 发起 PUT 请求
15. 请求 <https://httpbin.org/image/jpeg> 并保存为文件，然后使用你的图片编辑器打开这个文件
16. 请求 <https://www.twitter.com>，你将会得到空的响应。让 `curl` 显示出响应头信息，并尝试找出响应内容为空的原因
17. 向 <https://httpbin.org/anything> 发起任意的请求，同时设置一些无意义的请求头（例如：`panda: elephant`）
18. 请求 <https://httpbin.org/status/404> 和 <https://httpbin.org/status/200>，然后再次请求它们并且让 curl 显示响应头信息
19. 请求 <https://httpbin.org/anything> 并且设置用户名和密码（使用 `-u username:password`）
20. 设置 `Accept-Language: es-ES` 的请求头用以下载 Twitter 的西班牙语主页 (<https://twitter.com>)
21. 使用 `curl` 向 Stripe API 发起请求（请查看 <https://stripe.com/docs/development> 了解如何使用，他们会给你一个测试用的 API key）。尝试向 <https://httpbin.org/anything> 发起相同的请求




---


via: <https://jvns.ca/blog/2019/08/27/curl-exercises/>


作者：[Julia Evans](https://jvns.ca/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[hanwckf](https://github.com/hanwckf) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Recently I’ve been interested in how people learn things. I was reading Kathy Sierra’s great book [Badass: Making Users Awesome](https://www.amazon.com/Badass-Making-Awesome-Kathy-Sierra/dp/1491919019). It talks about the idea of *deliberate practice*.

The idea is that you find a small micro-skill that can be learned in maybe 3 sessions of 45 minutes, and focus on learning that micro-skill. So, as an exercise, I was trying to think of a computer skill that I thought could be learned in 3 45-minute sessions.

I thought that making HTTP requests with `curl`

might be a skill like that, so here are some curl
exercises as an experiment!

### what’s curl?

curl is a command line tool for making HTTP requests. I like it because it’s an easy way to test that servers or APIs are doing what I think, but it’s a little confusing at first!

Here’s a drawing explaining curl’s most important command line arguments (which is page 6 of my [Bite Size Networking](https://wizardzines.com/zines/bite-size-networking) zine). You can click to make it bigger.

### fluency is valuable

With any command line tool, I think having fluency is really helpful. It’s really nice to be able to just type in the thing you need. For example recently I was testing out the Gumroad API and I was able to just type in:

```
curl https://api.gumroad.com/v2/sales \
-d "access_token=<SECRET>" \
-X GET -d "before=2016-09-03"
```


and get things working from the command line.

### 21 curl exercises

These exercises are about understanding how to make different kinds of HTTP requests with curl. They’re a little repetitive on purpose. They exercise basically everything I do with curl.

To keep it simple, we’re going to make a lot of our requests to the same website:
[https://httpbin.org](https://httpbin.org). httpbin is a service that accepts HTTP requests and then tells you what request
you made.

- Request
[https://httpbin.org](https://httpbin.org) - Request
[https://httpbin.org/anything](https://httpbin.org/anything). httpbin.org/anything will look at the request you made, parse it, and echo back to you what you requested. curl’s default is to make a GET request. - Make a POST request to
[https://httpbin.org/anything](https://httpbin.org/anything) - Make a GET request to
[https://httpbin.org/anything](https://httpbin.org/anything), but this time add some query parameters (set`value=panda`

). - Request google’s robots.txt file (
[www.google.com/robots.txt](https://www.google.com/robots.txt)) - Make a GET request to
[https://httpbin.org/anything](https://httpbin.org/anything)and set the header`User-Agent: elephant`

. - Make a DELETE request to
[https://httpbin.org/anything](https://httpbin.org/anything) - Request
[https://httpbin.org/anything](https://httpbin.org/anything)and also get the response headers - Make a POST request to
[https://httpbin.org/anything](https://httpbin.org/anything)with the JSON body`{"value": "panda"}`

- Make the same POST request as the previous exercise, but set the Content-Type header to
`application/json`

(because POST requests need to have a content type that matches their body). Look at the`json`

field in the response to see the difference from the previous one. - Make a GET request to
[https://httpbin.org/anything](https://httpbin.org/anything)and set the header`Accept-Encoding: gzip`

(what happens? why?) - Put a bunch of a JSON in a file and then make a POST request to
[https://httpbin.org/anything](https://httpbin.org/anything)with the JSON in that file as the body - Make a request to
[https://httpbin.org/image](https://httpbin.org/image)and set the header ‘Accept: image/png’. Save the output to a PNG file and open the file in an image viewer. Try the same thing with different`Accept:`

headers. - Make a PUT request to
[https://httpbin.org/anything](https://httpbin.org/anything) - Request
[https://httpbin.org/image/jpeg](https://httpbin.org/image/jpeg), save it to a file, and open that file in your image editor. - Request
[https://www.twitter.com](https://www.twitter.com). You’ll get an empty response. Get curl to show you the response headers too, and try to figure out why the response was empty. - Make any request to
[https://httpbin.org/anything](https://httpbin.org/anything)and just set some nonsense headers (like`panda: elephant`

) - Request
[https://httpbin.org/status/404](https://httpbin.org/status/404)and[https://httpbin.org/status/200](https://httpbin.org/status/200). Request them again and get curl to show the response headers. - Request
[https://httpbin.org/anything](https://httpbin.org/anything)and set a username and password (with`-u username:password`

) - Download the Twitter homepage (
[https://twitter.com](https://twitter.com)) in Spanish by setting the`Accept-Language: es-ES`

header. - Make a request to the Stripe API with curl. (see
[https://stripe.com/docs/development](https://stripe.com/docs/development)for how, they give you a test API key). Try making exactly the same request to[https://httpbin.org/anything](https://httpbin.org/anything).