【笔记】IDEA发送HTTP请求

前言

通过IDEA发送HTTP请求,实现后段API接口测试

创建一个HTTP客户端

使用临时的http文件作为HTTP客户端

  • 如果只是临时使用可以直接打开一个临时的http文件作为HTTP客户端

  • Tools->HTTP Client->Create Request in HTTP Client

创建http文件

  • 如果想要保存发送请求的内容并保留响应结果,可以在项目中创建一个.http文件

  • 右键目录->New->HTTP Request->定义.http文件的文件名

发送HTTP请求

发送GET请求

1
2
GET http://localhost:80/api/item?id=99
Accept: application/json

发送POST请求

请求体传递JSON格式字符串

1
2
3
4
POST http://localhost:80/api/item
Content-Type: application/json

{}

请求体传递键值对字符串

1
2
3
4
5
POST http://localhost:80/api/item
Content-Type: application/x-www-form-urlencoded

id = 99 &
content = new-element

请求体传递文本表单

1
2
3
4
5
6
7
8
POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="field-name"

field-value
--WebAppBoundary--

请求体传递文件

1
2
3
4
5
6
7
8
POST http://localhost:80/api/item
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="field-name"; filename="file.txt"

< ./relative/path/to/local_file.txt
--WebAppBoundary--

完成

参考文献

CSDN——春花秋时知多少