The REST Client VS Code extension provides a nice and lightweight way to send HTTP requests. I usually use this as an alternative to Postman when I don't need the full power of Postman. This note contains some snippets that I use in my REST Client files.
Documenting HTTP requests in Markdown files
In Markdown files, it is possible to document HTTP requests using the fenced code block with the language http. The extension will add a “Send Request” button to the Markdown file which allows easily sending a request!
GET https://api.example.com/users
Form post
To send a form post, set: Content-Type: application/x-www-form-urlencoded. In the request body, each field can be on its own line.
POST https://api.example.com/login HTTP/1.1
Content-Type: application/x-www-form-urlencoded
name=foo
&password=bar
GraphQL
To send GraphQL request, set: X-REQUEST-TYPE: GraphQL then put the GraphQL query in the next section, followed by JSON representation of the variables to send along with the query.
POST https://api.github.com/graphql
Content-Type: application/json
Authorization: Bearer xxx
X-REQUEST-TYPE: GraphQL
query (…) {
…
}
{
/* variables here */
}
Referencing environment variables in .env files
You can use {{$dotenv ____}} to reference the environment value stored in the .env file in the same directory.
GET https://api.github.com/user
Authorization: Bearer {{$dotenv GITHUB_TOKEN}}
Prefixing
To not repeat the same URL over and over, variables can be used.
- Define variables with
@name = value - Reference variables with
{{ name }}
@base = https://api.example.com
GET {{base}}/user