My latest and most complete articles are in the pumpkin slow say www.pkslow.com, welcome to tea!

1 introduction

When doing Web application development, you often need to test the Web interface, so you need some clients to send HTTP requests to the server. Postman, curl, etc. After many times of use, I think VSCode REST Client is very easy to use, I would like to introduce to you.

It is a plug-in for VSCode that must be installed to use:

The main features are:

  • You can send requests and view returns directly in a text editor;
  • You can send the curl command directly.
  • You can copy HTTP requests as curl commands that can be executed on other Linux terminals.
  • You can manage request history: save, view, clear, etc.
  • Multiple requests on a file, with# # #Segmentation;
  • Can save the return body to the local;
  • Generate client code in multiple languages, such as Python /Java, etc.
  • Cookies function;
  • Most important: extremely powerful variable management capabilities.

Examples of these features are presented below.

2 Basic Functions

I’ll create a new file, pkslow. HTTP, with a suffix that can only be identified as HTTP or rest.

2.1 A link is a request

All you need is a link:

Nothing else, it says Send Request, and then click on it to Send a GET Request.

2.2 Multiple Requests

If you want multiple requests, split them with ###. Let’s proceed to define a more complete GET request.

https://www.pkslow.com

###
GET https://www.pkslow.com HTTP / 1.1
Copy the code

Click “Send Request”, the Request will be sent, and the return body will be on the right side:

POST request:

POST https://www.pkslow.com/user HTTP / 1.1
content-type: application/json

{
    "name": "larry"."age": 18
}
Copy the code

2.3 Copying the request to the curl command

Select the request, right-click it and select the following:

The results are as follows:

curl --request GET \
  --url https://www.pkslow.com/ \
  --header 'user-agent: vscode-restclient'
Copy the code

2.4 Sending curl

You can run cruL commands as follows:

2.5 Turn requests into client code

The operation is as follows:

We chose Python and the result is as follows:

import requests

url = "https://www.pkslow.com/"
headers = {'user-agent': 'vscode-restclient'}
response = requests.request("GET", url, headers=headers)
print(response.text)
Copy the code

3 Powerful variables

I’m discussing all the functionality of this variable because I think it’s too powerful. That’s one of the reasons why I chose it.

About variables:

Support for custom variables and environment variables;

Different environment variable definition and environment switching function;

Shared variables;

You can put variables anywhere: URL, Header, body, etc.

Get variables for other requests;

Provide system dynamic variables:

  • {{$guid}}
  • {{$randomInt min max}}
  • {{$timestamp [offset option]}}
  • {{$datetime rfc1123|iso8601 [offset option]}}
  • {{$localDatetime rfc1123|iso8601 [offset option]}}
  • {{$processEnv [%]envVarName}}
  • {{$dotenv [%]variableName}}
  • {{$aadToken [new] [public|cn|de|us|ppe] [<domain|tenantId>] [aud:<domain|tenantId>]}}

3.1 Custom variables in files

Define the variable with the @ command, and then reference it with two curly braces {{}} :

###
@pkslowHost = www.pkslow.com

GET {{pkslowHost}} HTTP / 1.1
Copy the code

3.2 Configuring Multiple Environment Variables

Configure multiple environment variables in vscode’s Settings file settings.json:

"rest-client.environmentVariables": {
  "$shared": {
    "username": "pkslow"."password": "123456"
  },
  "local": {
    "hostname": "localhost:8080"."password": "{{$shared password}}"
  },
  "production": {
    "hostname": "localhost:8081"."password": "{{$shared password}}"}}Copy the code

Add two local and production environments where the $shared variable can be referenced by everyone.

The reference variables hostname and password are as follows:

# # #POST http://{{hostname}}/auth/login HTTP / 1.1
Content-Type: application/json

{
    "username": "pkslow"."password": {{password}}
}
Copy the code

Switch the environment in the lower right corner of vscode:

3.3 Get the value of the return body of other requests as variables

Consider a scenario we often encounter where you first obtain a Token through a login request and then send the Token as a request to another request. This Token must be different every time you log in, and it would be very troublesome to obtain it every time, copy it, and paste it into subsequent requests. The REST Client provides a convenient approach to this scenario, so let’s see how to use it:

Obtain Token by login request first:

###
# @name loginAdmin
POST http://{{hostname}}/auth/login HTTP / 1.1
Content-Type: application/json

{
    "username": "pkslow"."password": {{password}}
}
Copy the code

We added an extra line:

# @name loginAdmin
Copy the code

Give it a name so you can reference its return body later.

Return the following body content:

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwa3Nsb3ciLCJyb2xlcyI6WyJST0xFX0FETUlOIl0sImlhdCI6MTYyMjI3OTMxMCwiZXhwIjoxNjIyMjc5OTEwfQ. h-fwUEOPx_tttlBOR8cXMHJWy2n6ath7lTqzfdAX87cCopy the code

So we’ll reference everything returned as follows:

### 
@token = {{loginAdmin.response.body.*}}
GET http://{{hostname}}/admin HTTP / 1.1
Authorization: Bearer {{token}}
Copy the code

Through the {{loginAdmin. Response. The body. *}} to refer to a variable. The format is as follows:

{{requestName.(response|request).(body|headers).(*|JSONPath|XPath|Header Name)}}

So if the returned body content is:

{
  "token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwa3Nsb3ciLCJyb2xlcyI6WyJST0xFX0FETUlOIl0sImlhdCI6MTYyMjI3OTMxMCwiZXhwIjoxNjIyMjc5OTEwfQ .h-fwUEOPx_tttlBOR8cXMHJWy2n6ath7lTqzfdAX87c"
}
Copy the code

By {{loginAdmin. Response. Body. $. Token}} to refer to a variable.

4 summarizes

GitHub VScode REST Client GitHub VScode REST Client


Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.