Interface simulation calls are often involved in back-end development. In addition to Postman, the REST Editor Client is a powerful tool for storing request content in a file in the code repository

1. Tool installation

Idea Plug-in Installation

Make an HTTP request

1. The request

A GET HTTP / 1.1 POST https://example.com/comments/1 https://example.com/comments/1 https://example.com/comments/1Copy the code

If the request method is omitted, the request is treated as GET

GET https://example.com/comments?page=2&pageSize=10
Copy the code

Sometimes there may be multiple query parameters in a single request, making it difficult to read and modify all query parameters in the request line. So we allow you to propagate query parameters to multiple rows (one query parameter in a row) and we will parse the row as soon as the request row starts,? And & like

GET https://example.com/comments
    ?page=2
    &pageSize=10
Copy the code

2. Request header

The line immediately following the request line of the first blank line is resolved as the request header. Please provide a title in the standard field-name: field-value format, with each line representing one title. By default, if not explicitly specified, the user-Agent header vscode-RestClient with a value is added to the REST Client Extension in the request. You can also change the default value rest-client.defaultheaders in the Settings. Here is an example of a request header:

User-Agent: rest-client Accept-Language: en-GB,en-US; Q = 0.8, en. Q = 0.6, useful - CN; Q = 0.4 the content-type: application/jsonCopy the code

3. Request body

If you want to provide the request body, add a blank line after the request line and the request header, after which it will be treated as the request body for all content. Here is an example of the request body:

POST https://example.com/comments HTTP / 1.1 the content-type: application/XML Authorization: token xxx <request> <name>sample</name> <time>Wed, 21 Oct 2015 18:27:50 GMT</time> </request>Copy the code

If the request body is large, you can write the request body in a file and reference the file example

POST http://dev.avatarinternalapi.jd.com/alarm/alarm-info
Content-Type: {{contentType}}

< ./alarm.json
Copy the code

Three variables.

1. Customize variables

Custom variables include environment variables file variables request variablesCopy the code
The environment variable

You can switch between different variables in different environments

Where to define: Settings -> Workspace Settings

{"rest-client.environmentVariables": {
    "$shared": {  // Share variables
        "version": "v1"
    },
    "local": {   // Local variables
        "version": "v2"."host": "dev.avatarinternalapi.jd.com"."token": "test token"
    },
    "production": {
        "host": "example.com"."token": "product token"}}}Copy the code

If there is no version in production, call the production environment and go back to the version in $shared

File variable
  • For file variables, the definition follows@variableName = variableValueSyntax that takes up the entire line.
  • Variable names must not contain any Spaces. As for the variable value, it can consist of any character, even allowing Spaces (leading and trailing Spaces will be stripped out).
  • If you want to reserve special characters like newlines, you can use backslashes\n
  • No matter where you define file variableshttpFiles that can be referenced in any request throughout the file
@host = dev.avatarinternalapi.jd.com
@contentType = application/json

GET  http://{{host}}/alarm/email-info?pin=zhangkaixuan100,zhangkaixuan1002

Content-Type: {{contentType}}
Copy the code
Request variable

The definition syntax for evaluating variables is like a single line comment, # @name newname before the desired request URL

Usage scenario: When a request requires the value of another request as the request parameter

The following request variable reference grammar {{requestName. (the response | request). (the body | headers). (JSONPath | XPath | Header Name)}}. You have two reference sections to select the response or request: the body and the heading. For the body part, which only works with JSON and XML responses, you can use JSONPath and XPath to extract specific attributes or attributes. For example, if the JSON response returns body {” ID “: “mock”}, you can set the JSONPath section $. Id to the reference ID.

Example:

### # @name createComment POST {{baseUrl}}/comments HTTP/1.1 Content-type: Application/json # # # # @ name getCreatedComment GET/comments / {{baseUrl}} {{createComment. Response. Body. $. Id}} HTTP / 1.1 Authorization: {{login.response.headers.X-AuthToken}}Copy the code

2. System variables

The system has some variables, use the system variables need to have the $sign

  • {{$guid}}Unique identification number
  • {{$randomInt min max}}Returns aminmaxRandom number between
  • {{$timestamp [offset option]}}Add:UTCThe time stamp.
  • {{$timestamp number option}}For example, 3 hours ago{{$timestamp -3 h}}; On behalf of the day after tomorrow{{$timestamp 2 d}}.

See the official documentation for more system variable usage

4. System Settings

You can override system Settings in user Settings

Example: “editor.fontSize”: 13, changing the system default fontSize to 13

Please refer to the official documentation for more user Settings

5. Rest Editor and Postman comparison

1. Install and start

  • postmanBelong to apcAn application must be downloaded and installed, and must be started each time it is used
  • rest editorBelongs to a plugin, high versionphpstormB: Yes, it isphpstomYou do not need to start a new application separately
  • Conclusion: In installation and startup,rest editorMore quickly

2. Operation and use

  • postmanThere are powerful operation interface, easy to use, support the definition of global variables. For ahttpAlmost all of the parameters required in the request can be defined in the interface.
  • rest editorThere is no interface. You need to define one.httpSuffixes the file in which the request is written. ahttpThe required parameters of the request, all need to be written manually, the general editor will have parameter association function.
  • Conclusion: Before compiling a completehttpIn the process of requesting,postmanA little more convenient; Both can save writtenhttprequest

3. Learning costs

  • Conclusion: In comparisonrest editorThe cost of learning is higher, because it is purely manual writing HTTP requests, so it needs to learnrest editorthehttpRequest authoring rules, and need to be proficienthttpThe agreement.

4. Team work

  • postman:You pay to share written HTTP requests with others
  • rest editorYou can synchronize your written files to the code repository for team sharing
  • Conclusion:rest editorEven more so

6. Other

1. The production of curl

Click on the Request and select Copy Request As cURL

curl --request POST --url http://dev.avatarinternalapi.jd.com/alarm/alarm-info --header 'content-type: application/json' --data '< ./alarm.json'
Copy the code

2. Use of shortcut keys

  • Ctrl+Alt+L (Cmd+Alt+L for macOS)Rerun the previous request
  • Ctrl+Alt+K (Cmd+Alt+K for macOS)Stop the request
  • Ctrl+Alt+E (Cmd+Alt+E for macOS)Choose an environment

For more information on the usage of shortcut keys, see the official documentation

3. Save the response

Save the complete response

Chart saved in upper left corner

Save the response body

Save the text chart in the upper left corner

4. Generate the original request snippet

After selecting all Code for a request -> right-click -> select Generate Code Sinppet -> select the language you want

Vii. Relevant information

The official documentation

Visual Studio Codedownload