失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > idea测试rest api方法(HTTP client in IntelliJ IDEA code editor)

idea测试rest api方法(HTTP client in IntelliJ IDEA code editor)

时间:2023-06-29 16:44:13

相关推荐

idea测试rest api方法(HTTP client in IntelliJ IDEA code editor)

最近更新了IDEA之后,发现了个新功能,可以用来测试rest api接口。之前写好的接口测试一直使用的Postman,发现在这个功能之后就把Postman完全抛弃了。

这是IDEA新版本的功能介绍HTTP client in IntelliJ IDEA code editor,链接先附上。

简单介绍

当你想测试你的rest api接口的时候,可以在你的IDEA当中直接创建、编辑、执行你的HTTP请求。这是IDEA官方帮助文档上找下的GIF。

IDEA使用快捷键Ctrl+Shift+Alt+Insert键可以选择建立一个HTTP请求文件,IDEA会自动把文件建立在Scratch文件夹下,当HTTP请求执行完成后,返回信息会以文件的形式保存在IDEA的顶部的历史文件中。

语法

Method Request-URI HTTP-VersionHeader-field: Header-valueRequest-Body

书写格式

请求文件中可以使用注释,使用// 或者 #

// A basic requestGET /a/

请求方法可以不写,默认使用为get一个请求文件中可以多个请求,使用 ### 隔开

// A basic request/a/###// A second request using the GET method:8080/api/html/get?id=123&value=content

请求过长可以断行,但要注意使用换行符

// Using line breaks with indentGET :8080/api/html/get?id=123&value=content

请求时可以使用身份认证,携带用户名和密码

// Basic authenticationGET Authorization: Basic username password###// Digest authenticationGET Authorization: Digest username password

POST请求时可以构建request的body信息

POST :8080/api/html/post HTTP/1.1Content-Type: application/json Cookie: key=first-value{ "key" : "value", "list": [1, 2, 3] }

可以实现文件的上传

POST /api/upload HTTP/1.1Content-Type: multipart/form-data; boundary=boundary--boundaryContent-Disposition: form-data; name="first"; filename="input.txt"// The 'input.txt' file will be uploaded< ./input.txt--boundaryContent-Disposition: form-data; name="second"; filename="input-second.txt"// A temporary 'input-second.txt' file with the 'Text' content will be created and uploadedText--boundaryContent-Disposition: form-data; name="third";// The 'input.txt' file contents will be sent as plain text.< ./input.txt --boundary--

IDEA给出的例子

get requests

### Get request with a headerGET /ipAccept: application/json### Get request with parameterGET /get?show_env=1Accept: application/json### Get request with environment variablesGET {{host}}/get?show_env={{show_env}}Accept: application/json### Get request with disabled redirects# @no-redirectGET /status/301###

auth resquests

### Basic authorization.GET /basic-auth/user/passwdAuthorization: Basic user passwd### Basic authorization with variables.GET /basic-auth/user/passwdAuthorization: Basic {{username}} {{password}}### Digest authorization.GET /digest-auth/realm/user/passwdAuthorization: Digest user passwd### Digest authorization with variables.GET /digest-auth/realm/user/passwdAuthorization: Digest {{username}} {{password}}### Authorization by token, part 1. Retrieve and save token.POST /postContent-Type: application/json{"token": "my-secret-token"}> {% client.global.set("auth_token", response.body.json.token); %}### Authorization by token, part 2. Use token to authorize.GET /headersAuthorization: Bearer {{auth_token}}###

post requests

### Send POST request with json bodyPOST /postContent-Type: application/json{"id": 999,"value": "content"}### Send POST request with body as parametersPOST /postContent-Type: application/x-www-form-urlencodedid=999&value=content### Send a form with the text and file fieldsPOST /postContent-Type: multipart/form-data; boundary=WebAppBoundary--WebAppBoundaryContent-Disposition: form-data; name="element-name"Content-Type: text/plainName--WebAppBoundaryContent-Disposition: form-data; name="data"; filename="data.json"Content-Type: application/json< ./request-form-data.json--WebAppBoundary--###

test responses

### Successful test: check response status is 200GET /status/200> {%client.test("Request executed successfully", function() {client.assert(response.status === 200, "Response status is not 200");});%}### Failed test: check response status is 200GET /status/404> {%client.test("Request executed successfully", function() {client.assert(response.status === 200, "Response status is not 200");});%}### Check response status and content-typeGET /get> {%client.test("Request executed successfully", function() {client.assert(response.status === 200, "Response status is not 200");});client.test("Response content-type is json", function() {var type = response.contentType.mimeType;client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'");});%}### Check response bodyGET /get> {%client.test("Headers option exists", function() {client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");});%}###

如果觉得《idea测试rest api方法(HTTP client in IntelliJ IDEA code editor)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。