学习009-05 Test the Web API with Swagger or Postman(使用Swagger或Postman测试Web API)

时间:2025-02-16 10:48:48

Test the Web API with Swagger or Postman(使用Swagger或Postman测试Web API

Use the Swagger UI to Test the Web API(使用Swagger UI测试Web API)

If your solution includes a Web API project, right-click the project in the Solution Explorer and choose Debug | Start new instance to run the Web API project. A browser displays the page with the available endpoints.

如果您的解决方案包含Web API项目,请在解决方案资源管理器中右键单击该项目并选择调试|启动新实例以运行Web API项目。浏览器会显示包含可用端点的页面。

If your solution includes a startup Blazor Server project that contains the Web API, run the application. Add /swagger to the application address (for example, https://localhost:44318/swagger ) and press Enter to display a page with available endpoints.

如果您的解决方案包含包含Web API的启动Blazor Server项目,请运行应用程序。将 /swagger添加到应用程序地址(例如,https://localhost:44318/swagger),然后按Enter显示包含可用端点的页面。

Refer to the following link for more information on the page's UI: Swagger UI.

有关页面UI的更多信息,请参阅以下链接:Swagger UI。

The default configuration starts the Web API service on different ports depending on the project:

默认配置根据项目在不同的端口上启动Web API服务:

Test the Web API. Expand the GET ApplicationUser endpoint and click the Try it out button. The Execute button is displayed. Click this button to see the result.

测试Web API。展开GET Application ationUser端点并单击试用按钮。显示执行按钮。单击此按钮可查看结果。

Use the Postman Tool to Test the Web API(使用Postman工具测试Web API)

You can also use the Postman tool to test the Web API. The Postman tool is more flexible and allows you to send complex requests with parameters to the Web API service. Refer to the following link for more information on how to utilize this tool: Sending your first request.

您还可以使用Postman工具来测试Web API。Postman工具更加灵活,允许您向Web API服务发送带有参数的复杂请求。有关如何使用此工具的更多信息,请参阅以下链接:发送您的第一个请求。

Tip

To test the Web API service hosted on localhost, install the Postman desktop agent as described in the following topic: Installing the Postman desktop agent.

要测试localhost上托管的Web API服务,请按照以下主题中的说明安装Postman桌面代理:安装Postman桌面代理。

The image below shows a request to the Contact business object filtered by FirstName in the Postman Web UI (/home):

下图显示了对Postman Web UI(/home)中由FirstName过滤的联系人业务对象的请求:

See the following topics for more information on OData query options:

有关OData查询选项的更多信息,请参阅以下主题:

  • Query options overview(查询选项概述)

  • Query options usage(查询选项使用)

If you enable authentication for your Web API service, specify the authorization settings for the Postman tool. See the following topic for details: Specifying authorization details. If the Web API service uses the XAF JWT token from the “Authenticate” endpoint, copy it from the Swagger UI page as described in the following section: Use the Swagger UI to Test the JWT Authentication.

如果为Web API服务启用身份验证,请指定Postman工具的授权设置。有关详细信息,请参阅以下主题:指定授权详细信息。如果Web API服务使用来自“身份验证”端点的XAF JWT令牌,请按照以下部分中的说明从Swagger UI页面复制它:使用Swagger UI测试JWT身份验证。