一、简介HTTPClient是IDEA自带的一个简单轻量级的接口调用插件。通过它,我们可以开发、调试、测试RESTfulweb服务。二、快速启动1、首先确保HTTPClient插件已经安装并启动,默认安装并启动。如果没有安装,请安装在File-Settings-Plugins的路径下:2.可以在项目根目录下新建一个文件夹存放请求文件,然后在里面新建一个HTTPClient请求文件:3.打开创建好的文件,可以直接点击右上角工具栏中的添加请求,选择对应的请求类型进行添加,如下图获取请求:4.点击左侧的运行按钮发送请求,结果如下:3.GET相关请求示例###GETrequestwithaheaderGEThttps://httpbin.org/ipAccept:application/json###GETrequestwithparameterGEThttps://httpbin.org/get?show_env=1Accept:application/json###GET带有环境变量的请求GET{{host}}/get?show_env={{show_env}}Accept:application/json###GET带有禁用重定向的请求#@no-redirectGEThttp://httpbin.org/status/301###GET带有动态变量的请求GEThttp://httpbin.org/anything?id={{$uuid}}&ts={{$timestamp}}###4。POST相关请求示例###发送POST请求,带jsonbodyPOSThttps://httpbin.org/postContent-Type:application/json{"id":999,"value":"content"}###发送POST请求正文作为参数POSThttps://httpbin.org/postContent-Type:application/x-www-form-urlencodedid=999&value=content###发送表单文本和文件字段POSThttps://httpbin.org/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--###在请求的正文中发送带有动态变量的请求POSThttps://httpbin.org/postContent-Type:application/json{"id":{{$uuid}},"price":{{$randomInt}},"ts":{{$timestamp}},"value":"内容”}###5。PUT相关请求示例PUThttp://localhost:8080/person/putContent-Type:application/json{"name":"name111","age":17}六、PATCH相关请求示例###PATCHhttp:///localhost:8080/person/putContent-Type:application/json{"name":"demo111","age":17}七、带认证验证的例子###PATCHhttp://localhost:8080/person/putContent-Type:application/json{"name":"demo111","age":17}VII.身份验证示例###基本授权。获取https://httpbin.org/basic-auth/user/passwdAuthorization:Basicuserpasswd###Basicauthorizationwithvariables.GEThttps://httpbin.org/basic-auth/user/passwdAuthorization:Basic{{username}}{{password}}###摘要授权。GEThttps://httpbin.org/digest-auth/realm/user/passwdAuthorization:Digestuserpasswd###Digestauthorizationwithvariables.GEThttps://httpbin.org/digest-auth/realm/user/passwdAuthorization:摘要{{username}}{{password}}###通过令牌授权,第1部分。检索并保存令牌。POSThttps://httpbin.org/postContent-Type:application/json{"token":"my-secret-token"}>{%client.global.set("auth_token",response.body.json.token);%}###Authorizationbytoken,part2.Usetokentoauthorize.GEThttps://httpbin.org/headersAuthorization:Bearer{{auth_token}}###八、断言方式请求示例###测试成功:检查响应状态为200GEThttps://httpbin.org/status/200>{%client.test("请求执行成功",function(){client.assert(response.status===200,"响应状态不是200");});%}###测试失败:检查响应状态为200GEThttps://httpbin.org/status/404>{%client.test("请求执行成功",function(){client.assert(response.status===200,"响应状态不是200");});%}###查看响应状态和content-typeGEThttps://httpbin.org/get>{%client.test("请求执行成功",function(){client.assert(response.status===200,"响应状态不是200");});client.test("响应内容类型为json",function(){vartype=response.contentType.mimeType;client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");});%}###检查响应正文GEThttps://httpbin.org/get>{%client.test("Headersoptionexists",function(){client.assert(response.body.hasOwnProperty("headers"),"在响应中找不到'headers'选项");});%}###
