如何开始使用Curl及其原因:动手介绍

by Luciano Strika

通过卢西亚诺·斯特里卡(Luciano Strika)

如何开始使用Curl及其原因:动手介绍 (How to start using Curl and why: a hands-on introduction)

Whether it’s testing the output of an API before deploying it to production, or simply fetching a response from a website (for instance, to check it’s not down), Curl is practically omnipresent.

无论是在将其输出到生产环境之前测试API的输出,还是只是从网站获取响应(例如,检查其是否正常),Curl实际上无处不在。

As a Data Scientist I’ve had to use it from time to time. However, more often than not I ended up just replacing parameters from a copied and pasted curl command that went around my team’s Slack channel.

作为数据科学家,我不得不不时使用它。 但是,最终我常常只是从围绕我团队的Slack通道复制和粘贴的curl命令替换参数。

I decided I needed to understand this powerful tool better if I wanted to use it to its full potential, and now I’m here to share some of the most interesting things I found in this curl tutorial.

我想,如果我想充分利用它的潜力,就需要更好地了解这个强大的工具,现在我在这里分享一些我在此curl教程中发现的最有趣的东西。

If you have any tips or tricks you’d like to add, please do so in the comments, as my understanding of this tool is still in its early stages.

如果您有任何要添加的提示或技巧,请在评论中添加,因为我对该工具的了解仍处于初期阶段。

Curl:这有什么用? (Curl: What is it good for?)

Curl is a command-line tool that allows us to do HTTP requests from shell. It also covers many other protocols, like FTP, though they go beyond the scope of this tutorial.

Curl是一个命令行工具,它使我们能够从shell发出HTTP请求。 它还涵盖了许多其他协议,例如FTP,尽管它们超出了本教程的范围。

Its name stands for “Client URL”, and it was developed by Swedish developer Daniel Stenberg. It is an open source project, and its code can be found here, in case you feel like contributing.

它的名称代表“客户URL” ,由瑞典开发人员Daniel Stenberg开发。 这是一个开源项目,可以在此处找到其代码,以备不时之需。

You can invoke it from your favorite terminal, and it usually comes pre-installed in Linux-based OS’s. Otherwise, it can normally be downloaded through apt-get on Linux, and brew on Mac.

您可以从自己喜欢的终端上调用它,它通常预装在基于Linux的操作系统中。 否则,通常可以在Linux上通过apt-get下载它,而在Mac上则可以brew

调用GET方法 (Calling a GET method)

In its most basic form, a curl command will look like this:

在其最基本的形式中,curl命令将如下所示:

curl http://www.dataden.tech

The default behavior for curl is to invoke an HTTP GET method on the given URL. This way, the program’s output for that command will be the whole HTTP response’s body (in this case, HTML) the site returns on a GET, which will be written as given on stdout.

curl的默认行为是在给定的URL上调用HTTP GET方法。 这样,该命令的程序输出将是站点在GET上返回的整个HTTP响应的主体(在本例中为HTML),该GET将按照stdout上的说明进行编写。

If you wish to read through a response without leaving the shell, I’d recommend at least piping it into a less command, to be able to easily scroll through the output.

如果您希望在不离开shell的情况下通读响应,则建议至少将其通过管道传递到less命令中,以便能够轻松滚动输出。

Many times we’ll wish to direct the response’s contents into a file. This is done with the -o argument, like this:

很多时候,我们希望将响应的内容定向到一个文件中。 这是通过-o参数完成的,如下所示:

curl -o output.html www.dataden.tech

which is equivalent to:

等效于:

curl www.dataden.tech > output.html

Optionally, you can specify the URL of the site you wish to call curl on with a -s argument, like this:

(可选)您可以使用-s参数指定要调用curl的站点的URL,如下所示:

curl -s http://www.dataden.tech

allowing you to change the order of your arguments.

允许您更改参数的顺序。

You can also use –next to specify more than one URL, though the official doc advises to instead call curl on each URL in a different command.

您也可以使用–next来指定多个URL,尽管官方文档建议改为使用不同的命令在每个URL上调用curl。

对URL执行POST (Doing a POST to a URL)

Sometimes you’ll want to test whether an API is working correctly, and usually that will require sending arguments to it.

有时,您需要测试API是否正常运行,通常这需要向其发送参数。

We’ll usually do this through the POST method, passing some JSON with all the required parameters. There are many ways to do this with curl.

通常,我们将通过POST方法执行此操作,并传递一些带有所有必需参数的JSON。 有很多方法可以做到这一点。

You can pass your arguments’ values like this:

您可以像这样传递参数的值:

curl --data "name=John&surname=Doe" http://www.dataden.tech

Or like a regular JSON:

或像常规JSON一样:

curl --data '{"name":"John","surname":"Doe"}' \http://www.dataden.tech

Using –data is equivalent to using -d, and both will make the method change to POST automatically. However, we can also use the -X flag (–request) to specify which method we want to invoke:

使用–data等同于使用-d,并且两者都会使方法自动更改为POST。 但是,我们也可以使用-X标志( -request )指定我们要调用的方法:

curl -X "POST" \-d "name=John&surname=Doe" http://www.example.com

取得网站标题 (Fetching the site’s headers)

Sometimes we just need to quickly see if the site’s still up, without really wanting to load the whole, potentially heavy, response. Other times, the headers store important configurations.

有时,我们只需要快速查看站点是否仍在运行,而无需真正加载整个潜在的沉重响应。 有时,标头存储重要的配置。

Those two use cases are also addressed by curl. We can use the –include (-i) parameter to include the headers, and –head (-I -that’s capital ‘i’-) to include only the headers (calling the HEAD method).

这两个用例也可以通过curl解决。 我们可以使用-include(-i)参数包括报头,并-head(-I -这就是资本“i'-),只包括头(调用HEAD方法)。

设置用户代理值 (Setting your user-agent value)

Now that I’ve covered the basics, let me walk you through some of the coolest things I’ve found we can do with curl.

既然我已经介绍了基本知识,那么让我向您介绍一些我发现可以使用curl进行的最酷的操作。

The user-agent argument lets you specify which device and browser versions you are using, in case that makes the site render differently. With this, you could see the mobile version of a site from your laptop, or vice versa.

使用user-agent参数可以指定正在使用的设备和浏览器版本,以防站点呈现不同的情况。 这样,您可以从笔记本电脑上看到网站的移动版本,反之亦然。

From a security standpoint, this probably raises some issues. I didn’t know how easy it is to pretend to be using a different device (without even using a virtual machine) until now, and working in Fraud Prevention I can see why this could be a problem.

从安全角度来看,这可能会引起一些问题。 直到现在,我还不知道假装使用其他设备(甚至不使用虚拟机)是多么容易,并且在“欺诈预防”中工作,我明白了为什么这可能是个问题。

With that said, as long as you are using this for good, this is an awesome way of seeing what a website looks like from a tablet, a mobile device or a laptop, to name a few.

话虽如此,只要您永远使用它,这就是从平板电脑,移动设备或笔记本电脑看网站的一种好方法,仅举几例。

Here’s an example, straight from the official documentation (though lists of user-agents are readily available online).

这是一个直接来自官方文档的示例(尽管用户代理列表可随时在线获得)。

curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" www.example.com

计时与Curl的连接 (Timing a connection with Curl)

Another reason I started learning more about curl was that I wanted to see how long exactly it took for my website to respond.

我开始学习更多有关curl的另一个原因是,我想看看我的网站需要花多长时间才能做出响应。

Though the basic documentation doesn’t cover it, a bit of googling uncovered this command, which I’ve found very useful:

尽管基本文档没有介绍它,但是在谷歌搜索中发现了该命令,我发现它非常有用:

curl -w "%{time_total}\n" -o /dev/null -s www.example.com

This will simply output the total time it took to fetch the response from the given domain.

这将仅输出从给定域获取响应所花费的总时间。

More generally, the -w (–write-out) argument takes a special formatting string, and fills in reserved keywords with different properties of the response, in a formatted way. All keywords, and their respective values, are available in the command’s man page.

更一般而言, -w(–write-out)参数采用特殊的格式设置字符串,并以格式化的方式填充具有不同响应属性的保留关键字。 所有关键字及其各自的值在命令的手册页中都可用。

进一步阅读 (Further reading)

Here are a few links you may find interesting, in case you wish to learn more about this broad subject:

如果您想进一步了解这个广泛的主题,以下一些链接可能会很有趣:

总结一下 (To conclude)

I hope you’ve found this introduction useful, and you leave this tutorial knowing at least the basics of this convenient command.

我希望您发现本介绍很有用,并且使本教程至少了解此便捷命令的基础。

As I said before, I’m still learning as well, and will appreciate any other interesting bits of knowledge about the program’s use. The same goes for any feedback on what I’ve written so far.

就像我之前说过的那样,我仍在学习,并且将感谢您对该程序的使用有任何其他有趣的认识。 关于我到目前为止所写内容的任何反馈也是如此。

If I’ve made any mistakes, or there’s any part you think I could have worded more clearly, please let me know.

如果我有任何错误,或者您认为我可以在措辞上更清楚一些,请告诉我。

I hope I’ll see you again soon, happy coding!

希望我能再见到你,编码愉快!

Follow me on Medium and Twitter to keep up to date with my tutorials, tips and articles. Consider sharing this article with a web developer friend if you liked it (or as a passive aggressive way of telling them to learn curl).

MediumTwitter上关注我,以了解我的教程,技巧和文章的最新信息。 如果喜欢,请考虑与Web开发人员朋友分享此文章(或作为一种告诉他们学习卷曲的被动的积极方式)。

Originally published at www.dataden.tech on October 7, 2018.

最初于2018年10月7日发布在www.dataden.tech

翻译自: https://www.freecodecamp.org/news/how-to-start-using-curl-and-why-a-hands-on-introduction-ea1c913caaaa/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值