1. 使用步骤
在 VS Code 新建一个以 .http 或者 .rest 结尾的文件,填入你的 HTTP 请求,点击 Send Request,或者右键选择 Send Request,或者直接用快捷键 Ctrl + Alt + R ,你的 REST API 就执行了,然后 API Response 就会显示在右边区域。
image
编写 api 文档可以直接用 charles 抓包,然后选择 Contents -> Raw ,拷贝其中的内容直接放到 .http 文件中即可
2. HTTP 请求
使用 @apiHost 的 apiHost 变量做为动态域名
@apiHost = xxx.xxxdm.net
Get 请求
http://{{ apiHost }}//vx/list?offset=0&limit=20
或
GET /app/xx/index.php?r=xxx/ddd&latitude=&accessToken=sfa&longitude=&accessSecret=jjhf HTTP/1.1
X-Client: android/3.5.3
Host: {{ apiHost }}
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
Connection: keep-alive
Post 请求
content-type 为 application/x-www-form-urlencoded
POST /vx/like/count HTTP/1.1
Host: {{ xxxHost }}
Content-Type: application/x-www-form-urlencoded; charset=utf-8
X-Auth-Token: sdfa
Content-Length: 95
Accept: application/json, text/plain,image/webp, */*
User-Agent: djxxx/4.6.8 (xxx.ddd.xxxdm; build:1.0.0; iOS 13.5.0) Aladire/4.9.1
Accept-Language: zh-Hans-CN;q=1.0
X-Client: iOS/2.1.2-beta
Accept-Encoding: gzip;q=1.0, compress;q=0.5
Connection: keep-alive
accessSecret=jjhf&accessToken=sfa&tid=123
content-type 为 application/json
POST http://dummy.restapiexample.com/api/v1/create HTTP/1.1
content-type: application/json
{
"name":"Hendry",
"salary":"61888",
"age":"26"
}
content-type 为其他
参考文档
3. 一个文件包含多个请求
通过 ### 分隔符,同一个 HTTP 文件里可以涵盖多个HTTP请求。不像Postman,不同的HTTP请求需要放在不同的tab里
image
4. 动态域名
使用 @xxxHost 的 xxxHost 变量做为动态域名
@xxxHost = xxx.xxxdm.net
使用动态域名:
http://{{ xxxHost }}//v2/acg/series-list?offset=0&limit=20&zq_id=4&zqxl_id=1
或
GET /mobcent/app/web/index.php?r=xxx/atuserlist&latitude=&accessToken=sfa&longitude=&accessSecret=jjhf HTTP/1.1
X-Client: android/3.5.3
Host: {{ apiHost }}
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
Connection: keep-alive
5. 代码生成
可以方便地通过 Generate Code Snippet 命令来把 HTTP 请求生成出不同编程语言的代码:JavaScript, Python, C, C#, Java, PHP, Go, Ruby, Swift 等等主流语言。
image