Introduction:
Visual Studio, the popular integrated development environment (IDE) by Microsoft, continually evolves to enhance developers' productivity and streamline their workflows. In the latest release, a new feature has been introduced: .http files. These files provide a convenient way to interact with HTTP APIs directly within Visual Studio. In this blog post, we will delve into the details of .http files, explore code examples in C#, discuss the system dynamic variables available, and highlight their similarity to the Visual Studio Code extension, "REST Client."
Understanding .http Files:
.HTTP files in Visual Studio act as a lightweight HTTP client, allowing developers to write and execute HTTP requests directly in the IDE. These files are inspired by the Visual Studio Code extension called "REST Client" and provide a similar experience within Visual Studio itself. They are designed to simplify the process of testing and debugging HTTP APIs.
Creating an .http File:
To create an .http file in Visual Studio, follow these steps:
- Right-click on your project or desired location in the Solution Explorer.
- Choose "Add" and then "New Item."
- Select "Text File" from the available templates and name it with the .http extension.
Writing HTTP Requests in C#:
Let's explore some code examples to demonstrate how to use .http files effectively in Visual Studio using C#. Assume we have an API endpoint at "https://api.example.com/users" that returns a list of users in JSON format.
Sending a GET Request:
GET https://api.example.com/users
HTTP/1.1
Host: api.example.com
Sending a POST Request:
POST https://api.example.com/users
Content-Type: application/json
{
"name": "John Doe",
"email": "john.doe@example.com"
}
System Dynamic Variables in .http Files:
Visual Studio's .http files provide a set of system dynamic variables that you can utilize to enhance your HTTP requests. These variables simplify the process of including dynamic values in your requests, such as environment-specific URLs or authentication tokens.
Here are the system dynamic variables available in .http files:
{{guid}}
: Generates a new GUID for each request.{{randomInt}}
: Generates a random integer for each request.{{timestamp}}
: Represents the current timestamp.{{date}}
: Represents the current date.{{time}}
: Represents the current time.{{utcDate}}
: Represents the current UTC date.{{utcTime}}
: Represents the current UTC time.{{month}}
: Represents the current month.{{year}}
: Represents the current year.{{host}}
: Represents the current host URL.
Example using system dynamic variables:
POST https://api.example.com/users
Content-Type: application/json
{
"id": "{{guid}}",
"name": "John Doe",
"email": "john.doe@example.com"
}
###
GET https://api.example.com/logs?timestamp={{timestamp}}
###
Conclusion:
With the introduction of .http files in Visual Studio, developers now have a convenient way to interact with HTTP APIs directly within their IDE. These files provide a familiar and intuitive syntax, making it easier to test and debug APIs without leaving the development environment. By utilizing system dynamic variables, developers can enhance their HTTP requests and streamline their workflows further. .http files in Visual Studio resemble the functionality of the "REST Client" extension in Visual Studio Code, providing a seamless experience across platforms.
Start leveraging this powerful feature today and experience the productivity boost it offers!
Happy coding with Visual Studio and .http files!
(Note: The code examples provided in this blog are for demonstration purposes only. Actual implementation may vary depending on the specific HTTP API and requirements.)
【译】HTTP 文件更新了请求变量
许多用户都要求在 Visual Studio 的 HTTP 文件中添加对请求变量的支持。使用请求变量,您可以发送 HTTP 请求,然后在从 HTTP 文件发送的任何后续请求中使用响应或请求中的数据。我们还添加了对共享环境 $shared 的支持,它使您能够在不同的环境中共享变量。在这篇文章中,我们将概述对请求变量等新添加的支持。本文中列出的所有功能都包含在 Visual Studio 2022 17.12+ 中。
请求变量
在使用 API 时,通常会从终结点获取一个值,然后在后续请求中使用该值。这可以通过使用请求变量来实现。我们有请求变量的文档,但我们也会在这里讨论所有内容。使用请求变量的一个更常见的场景是,当您调用终结点对 API 进行身份验证并获得可用于未来请求的令牌时。下面的示例请求是 David Fowler 的 TodoApi 示例。该 API 有一个终结点,您可以通过提供用户名和密码来创建一个新用户。这是我们正在发出请求的终结点。
@username = bloguser
# login and save the response as "login"
# @name login
POST {{TodoApi_HostAddress}}/users/token
Content-Type: application/json
{
"username": "{{username}}",
"password": "{{password}}"
}
###
在这种情况下,用户名是在 HTTP 文件中定义的,但密码是使用 HTTP Environments 安全地存储的。下面的请求是发送给 /users/token 终结点的,我们将用户名和密码作为 HTTP 请求主体的一部分传入。使其成为请求变量(有时也称为命名请求),特殊之处在于注释上方的那一行。
# @name login
在 Visual Studio 中发送此请求后,您可以从响应或请求中获取值。在下面的代码片段中,您可以看到我们如何使用登录中的请求变量来访问在提交响应时作为响应的一部分返回的令牌。登录的响应包含一个 token。现在我们已经登录了,我们可以用下面的请求创建一个 TODO 项。
# Create a TODO item
# @name todo1
POST {{TodoApi_HostAddress}}/todos
Authorization: Bearer {{login.response.body.$.token}}
Content-Type: application/json
{
"title": "Write blog post"
}
###
在此请求中,我们提取 token 值并使用它来指定 Authorization 头的值。语法为 {{login.response.body.$.token}}。让我们仔细看看语法。
{{login.response.body.$.token}}
下面的表格总结了使用来自请求变量的值的语法:
元素 |
描述 |
---|---|
requestVarName |
被引用的请求变量。 |
response|request |
是否从响应或请求中提取值。 |
body|headers |
是否从请求或响应的报头或正文中提取值 |
*|JSONPath|XPath|Header |
计算表达式,用于结果提取对于返回 JSON 主体的请求,使用 JSONPath 表达式。对于返回 XML 主体的请求,使用 XPath。* 将返回整个结果。当从 header 中提取时不能使用*。 |
对于上面的示例请求,我们从响应中提取令牌,并将其作为请求的标头传递给 /todos 终结点。发送此请求后,返回的结果如下所示:
{
"id": 36,
"title": "Write blog post",
"isComplete": false
}
现在我们已经创建了一个 TODO 项目,我们可以用下面的请求更新该项目。如果您注意到上面的请求声明了一个名为 todo1 的请求变量,那么我们可以使用它来引用响应或请求中的值。让我们更新标题,在当前标题的末尾添加“today”。下面的请求将更新 TODO 项。我们将使用 PUT,因为这是对现有项的更新。
PUT {{TodoApi_HostAddress}}/todos/{{todo1.response.body.$.id}}
Authorization: Bearer {{login.response.body.$.token}}
Content-Type: application/json
{
"id": {{todo1.response.body.$.id}},
"title": "{{todo1.response.body.$.title}} today",
"isComplete": {{todo1.response.body.$.isComplete}}
}
###
在这个请求中,我们使用来自原始 todo1 请求的数据填充 PUT 请求的主体。注意,title 属性将“today”附加到现有标题的末尾。发送此请求后,结果为:
{
"id": 36,
"title": "Write blog post today",
"isComplete": false
}
在这里,您可以看到博客文章的标题已成功更新。在这些示例中,我展示了如何处理“扁平”的 JSON 结果,但是您可以使用任何 JSONPath 表达式从响应或请求体中提取数据。如果终结点返回 XML,请使用 XPath 表达式而不是 JSONPath。让我们继续讨论对 $shared 的支持。
$shared
在使用 HTTP 环境时,您可以为 HTTP 请求定义多个不同的环境。例如,您可以创建一个引用本地运行的 API 的开发环境,当您想要向远程测试环境发送请求时创建一个测试环境。在这些情况下,您可能希望声明一个对所有环境都可用的变量。这正是新的 shared环境所要做的。HTTP环境定义在一个名为http−client.env.json或http−client.env.json.user的文件中。如果您