postman api使用_如何使用Postman掌握您的API工作流程

postman api使用

Building good APIs is hard, and anyone who had the chance to do so can relate to this. A project can easily grow to become a mess. One can keep trying to adopt an approach to make it more enjoyable, like trying a documentation-first workflow, but something always feels clumsy.

构建良好的API很难,任何有机会这样做的人都可以与此相关。 一个项目很容易成长为一团糟。 可以继续尝试采用一种使它变得更有趣的方法,例如尝试以文档为先的工作流程,但是总有些笨拙。

I was trying out Postman lately, a tool we’ve briefly covered before, and I discovered that they’re doing a great job by providing an integrated environment for different components of an API, like authorization, testing, documentation, versioning, etc.

我最近尝试了Postman ,这是我们之前已经简要介绍过的工具,我发现它们通过为API的不同组件(例如授权,测试,文档,版本控制等)提供集成环境,正在做得很好。

Postman logo

In this article, we’re going to walk through the different features that Postman provides and how we can organize them to make our API workflow less painful.

在本文中,我们将逐步介绍Postman提供的各种功能,以及如何组织它们以减轻API工作流程的痛苦。

发出请求 (Making Requests)

The first step to discovering Postman is making a simple API request and seeing the response.

发现Postman的第一步是发出一个简单的API请求并查看响应。

Simple request

From the screenshot above we can see that the view incorporates different elements. The top bar contains the request method (in this case GET), and right next to it we specify the endpoint to make the request to. If the request has some params we can specify them by clicking the Params button, which will open a table for entering them. When ready, we can click the send button to initiate the request.

从上面的屏幕截图中,我们可以看到视图包含了不同的元素。 顶部栏包含请求方法(在本例中为GET ),在其旁边,我们指定了向其发出请求的端点。 如果请求中包含一些参数,我们可以通过单击“ 参数”按钮来指定它们,这将打开一个用于输入它们的表格。 准备就绪后,我们可以单击发送按钮以发起请求。

The next section contains five tabs:

下一部分包含五个选项卡:

  • Authorization: Specifies how to authorize the request. Like Basic Auth, OAuth2, etc.

    授权:指定如何授权请求。 像Basic AuthOAuth2等。

  • Headers: Any header that should be sent with the request. Like content-type, Authorization, etc.

    标头:应与请求一起发送的任何标头。 如content-typeAuthorization等。

  • Body: Request body for request types like Post, PUT, etc.

    正文:请求正文,例如PostPUT等。

  • Pre-request Script: JS code to be executed before performing the request. (More about this later)

    请求前脚本:执行请求之前要执行的JS代码。 (稍后对此有更多介绍)
  • Tests: JS code to validate the response payload.

    测试:JS代码以验证响应有效负载。

The bottom section contains all the details about the response (status, time and size). The four tabs are self explanatory, except for Tests which contains the tests result if we have any (more about this later).

底部包含有关响应的所有详细信息( statustimesize )。 这四个选项卡不言自明,“ Tests除外,其中包含测试结果(如果有的话)(稍后会对此进行更多介绍)。

授权书 (Authorization)

Postman supports all types of authorization, as we saw above. We’re going to focus on token authorization set via header. You can read more about authorization here.

如上所述,邮递员支持所有类型的授权。 我们将重点介绍通过标头设置的令牌授权。 您可以在此处阅读有关授权的更多信息。

The header format that Postman will use is:

Postman将使用的标头格式为:

Authorization: Bearer <TOKEN>
Authorization header

Now, we’ll go ahead and get a token we can use for our future requests. I’m testing on an in-development application of mine, but you can use any application you have lying around, or set one up quickly with Laravel as described here.

现在,我们将继续获取可用于将来请求的令牌。 我正在测试我正在开发的应用程序,但是您可以使用周围的任何应用程序,也可以按照此处所述使用Laravel快速设置一个应用程序。

Not familiar with Laravel? Try our premium course – it’s a comprehensive introduction, and you’ll get the upcoming newer version of it automatically if you grab it in its current iteration!

不熟悉Laravel? 尝试我们的高级课程 -这是一个全面的介绍,如果您在当前版本中掌握它,就会自动获得即将推出的较新版本!

Get token

At this point, we have a token to be used for querying data from the API as an authenticated user. However, copying and pasting the token for every request is tedious!

在这一点上,我们已经有了一个令牌,该令牌可以作为经过身份验证的用户从API查询数据。 但是,为每个请求复制和粘贴令牌很繁琐!

环境变量 (Environment Variables)

This nice feature alleviates the pain of copy/paste and groups our variables in one place. An environment is an execution context: we may have a local environment, testing, staging, etc.

这个不错的功能减轻了复制/粘贴的麻烦,并将我们的变量集中在一个位置。 环境是执行上下文:我们可能具有本地环境,测试,暂存等。

Postman has different scopes for variables:

邮递员对变量有不同的范围:

  • Global

    全球
  • Environment

    环境
  • Local

    本地
  • Data

    数据

The global variables are available everywhere, separately from the selected environment. You can read more in the

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值