拨测工具_您可以拨多少钱? 快速简单地介绍有用的工具。

拨测工具

by Miguel Bustamante

通过Miguel Bustamante

您可以卷曲多少? 快速简单地介绍有用的工具。 (How much can you cURL? A quick and easy intro to a useful tool.)

On a good day I can flex a 20 lb weight…twice. Probably. But that’s not the type of curling we’re talking about!

在美好的一天,我可以弯曲20磅的重量……两次。 大概。 但这不是我们在谈论的那种卷发!

Curl (or cURL), on the other hand, is a small but powerful tool for transferring files and data over URLs. On a smaller scale, it’s great for testing REST APIs. And, though most web developers might opt to use other tools such as Google’s Postman, cURL is done in the command line and can leave you feeling like a true computer hack with David Lightman-like skills (for you “War Games” fans).

另一方面,Curl(或cURL)是一个小型但功能强大的工具,用于通过URL传输文件和数据。 在较小的规模上,它非常适合测试REST API。 而且,尽管大多数Web开发人员可能选择使用其他工具,例如Google的Postman ,但cURL是在命令行中完成的,可以让您感觉像是具有David Lightman般技巧的真正的计算机黑客(对您来说是“战争游戏”的粉丝)。

CURL stands for “client” and “URL”, since it is a program run on the client side that makes HTTP requests to URL’s. Since it’s open source, you can download it here. Or if you have Gitbash already installed on your machine, it’s automatically included.

CURL代表“客户端”和“ URL”,因为它是在客户端运行的程序,该程序向URL发出HTTP请求。 由于它是开源的,您可以在此处下载。 或者,如果您的计算机上已经安装了Gitbash ,它将自动包含在内。

For the purposes of this quick intro, we’ll need a server that’ll allow us to make requests, and it seems JSON Placeholder fits our needs nicely. It is a fake REST API that, even though our requests won’t actually change the server’s database, will still give us the appropriate response. So go ahead and crack open that console and let’s get hacking!

出于本快速介绍的目的,我们需要一个允许我们进行请求的服务器,并且JSON Placeholder似乎很好地满足了我们的需求。 这是一个伪造的REST API,即使我们的请求实际上不会更改服务器的数据库,也仍会给我们适当的响应。 因此,继续破解该控制台,让我们开始吧!

得到 (Get)

To start, we’ll try a simple HTTP “get” request. Scroll down to the “Resources” section in JSON placeholder and let’s take a look at the types of objects we can make a request on.

首先,我们将尝试一个简单的HTTP“ get”请求。 向下滚动到JSON占位符中的“资源”部分,让我们看一下可以请求的对象类型。

Nice! We can call these objects by adding “/”, then the object we want in the URL. The number on the right of the row tells us how many items we’ll get back with this request. For starters, let’s request some users. Type the following line into the console:

真好! 我们可以通过添加“ /”,然后在URL中添加所需的对象来调用这些对象。 该行右边的数字告诉我们此请求将退还多少物品。 首先,让我们请求一些用户。 在控制台中输入以下行:

curl https://jsonplaceholder.typicode.com/users

You should see all ten users we were promised as JSON objects. But maybe I just want the fifth user. We’ll add “/5” after the URL to get the user with the I.D. of 5.

您应该看到我们被承诺为JSON对象的所有十个用户。 但是也许我只想要第五个用户。 我们将在URL后面添加“ / 5”,以获取ID为5的用户。

curl https://jsonplaceholder.typicode.com/users/5

We see the JSON object for the fifth user. Great, let’s try to post a user to the server.

我们看到了第五个用户的JSON对象。 很好,让我们尝试将用户发布到服务器。

发布 (Post)

“Post”- ing is the process of submitting data to the server and having it be saved in the database. To do this with cURL let’s look at its options. Type:

“发布”是将数据提交到服务器并将其保存在数据库中的过程。 为此,请看一下cURL。 类型:

curl --help

and you should get a bunch of cool options we can use in the terminal:

您应该可以在终端中使用很多不错的选择:

For our purposes, it looks like the “-d” or “- -data” option would work nicely. If we look back at the homepage of the placeholder, in the “Routes” section, it tells us we could make a post request to “https://jsonplaceholder.typicode.com/posts”. With this information, we’ll post our own object through the console:

就我们的目的而言,“-d”或“--data”选项似乎可以很好地工作。 如果我们回顾占位符的主页,在“路由”部分,它告诉我们可以向“ https://jsonplaceholder.typicode.com/posts ”发出发布请求。 有了这些信息,我们将通过控制台发布我们自己的对象:

curl -d "title=Greatest Post Ever Written&body=Body of the Greatest post ever written" https://jsonplaceholder.typicode.com/posts

Now you’ll see the post being “created” in the db, and it has an I.D. of 101.

现在,您将在数据库中看到该帖子正在“创建”,并且其ID为101。

更新资料 (Update)

Sometimes we need to change objects in the db. We can only change things already saved in the database, and since this is a fake REST API, our post wasn’t actually saved. So lets update a post that exists. How about the 56th one. Type:

有时我们需要更改数据库中的对象。 我们只能更改已经保存在数据库中的内容,并且由于这是一个伪造的REST API,因此我们的帖子实际上并未保存。 因此,让我们更新存在的帖子。 第56个怎么样? 类型:

curl https://jsonplaceholder.typicode.com/posts/56

And you’ll see:

您会看到:

It’s saved with some funky Lorem Ipsum text that we should probably change to something intelligible. We are going to need a few other options with our command here. First, we’ll need to tell cURL that it’s a “put” request. So as we look through our “- -help” option, it seems we could use “-X” to tell cURL we want to use a “PUT” command.

它与一些时髦的Lorem Ipsum文本一起保存,我们可能应该更改为可理解的文本。 在这里,我们的命令将需要其他一些选项。 首先,我们需要告诉cURL这是一个“放置”请求。 因此,当我们浏览“--help”选项时,似乎可以使用“ -X”告诉cURL我们要使用“ PUT”命令。

Then we still want to use the “-d” option for the new data we intend to use. Let’s piece it all together. Type:

然后,我们仍然要对要使用的新数据使用“ -d”选项。 让我们拼凑起来。 类型:

curl -X PUT -d "title=This is a new title" https://jsonplaceholder.typicode.com/posts/56

And just like that, we have changed the title of the post with the I.D. of 56 to what we wanted.

就像这样,我们将ID为56的帖子标题更改为所需的标题。

删除 (DELETE)

And now we come to the delete. Ahh, the delete. If all else fails, destroy it all! We are going to see some of the same code as we saw in the PUT command, but all we need is to give cURL a DELETE request and the URL to the post we are to delete.

现在我们来删除。 啊,删除。 如果其他所有方法均失败,则将其全部销毁! 我们将看到与在PUT命令中看到的相同的代码,但是我们所需要的只是给cURL一个DELETE请求和要删除的帖子的URL。

curl -X DELETE https://jsonplaceholder.typicode.com/posts/56

Notice that you get nothing back in return but a newline. Maybe on some consoles you’ll see and empty hash(“{}”). This indicates that there is nothing to return because it was deleted.

注意,除了换行符之外,您什么也没有得到回报。 也许在某些控制台上您会看到并清空hash(“ {}”)。 这表明没有东西要返回,因为它已被删除。

结语 (Wrapping up)

We only touched on some cURL commands at a very superficial level. It is a neat tool that can be helpful when working on fully functioning API integration in your app. I would suggest looking at the manual for further reading and playing around with the different options to see what may fit your needs.

我们仅在非常肤浅的层次上触及了一些cURL命令。 这是一个简洁的工具,当您在应用程序中进行功能全面的API集成时可能会有所帮助。 我建议您查看手册,以进一步阅读并尝试使用不同的选项,以了解可能适合您的需求。

翻译自: https://www.freecodecamp.org/news/how-much-can-you-curl-3c88e2fed3f6/

拨测工具

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值