IDEA

1、打开IDEA中的HTTP Client

image.png
image.png

2、构建基本的HTTP请求

HTTP Client中HTTP请求的结构

  • 第一行:每个HTTP请求的脚本需要###作为分隔符或者注释(每个文件中的第一个请求可以不用###来表示)
  • 第二行:请求的方法类型(GET、POST、DELETE、PUT等支持Restful风格的请求)、使用空格将请求的方法类型和URL分开
  • 第三行:第三行开始是请求头的设置,以key,value的格式进行设置
  • 数据体Body:在请求头设置完成后隔一行设置数据体body

image.png

:::info 每个HTTP请求需要以###作为分割符,否则不能识别
正确的做法
image.png ::: :::danger 错误的做法示例
image.png :::

GET请求

  1. ### GET request with a header
  2. GET https://httpbin.org/ip
  3. Accept: application/json
  1. ### GET request with parameter
  2. GET https://httpbin.org/get?show_env=1
  3. Accept: application/json
  1. ### GET request with environment variables
  2. GET {{host}}/get?show_env={{show_env}}
  3. Accept: application/json
  1. ### GET request with disabled redirects
  2. # @no-redirect
  3. GET http://httpbin.org/status/301
  1. ### GET request with dynamic variables
  2. GET http://httpbin.org/anything?id={{$uuid}}&ts={{$timestamp}}
  3. ###

POST请求

:::danger 请求体的Body数据需要在请求头(Request Header)下面隔一行写才能被识别 :::

JSON格式的请求数据体

  1. POST https://httpbin.org/post
  2. Content-Type: application/json;charset=UTF-8
  3. {
  4. "id": 999,
  5. "value": "content"
  6. }

拼接参数的请求数据体

  1. POST https://httpbin.org/post
  2. Content-Type: application/x-www-form-urlencoded
  3. id=999&value=content

文字或者文本文件格式的请求数据体

  1. POST https://httpbin.org/post
  2. Content-Type: multipart/form-data; boundary=WebAppBoundary
  3. --WebAppBoundary
  4. Content-Disposition: form-data; name="element-name"
  5. Content-Type: text/plain
  6. Name
  7. --WebAppBoundary
  8. Content-Disposition: form-data; name="data"; filename="data.json"
  9. Content-Type: application/json
  10. < ./request-form-data.json
  11. --WebAppBoundary--

文件类型的请求数据体

  1. POST http://localhost:8085/wm/file/upload/OSS
  2. Content-Type: multipart/form-data; boundary=WebAppBoundary
  3. --WebAppBoundary
  4. Content-Disposition: form-data; name="file"; filename="areaPoint.png"
  5. < src/main/resources/img/areaPoint.png
  6. --WebAppBoundary--

通过WebAppBoundary进行数据的绑定,通过左箭头将文件写入
image.png

3、配置HTTP Client的变量定义及使用

①创建变量配置文件

image.png

②在配置文件配置变量值

  1. {
  2. "dev": {
  3. "baseUrl": "http://localhost:4000"
  4. },
  5. "uat": {
  6. "baseUrl": "http://117.62.135.253:4000"
  7. }
  8. }

image.png

③在请求中使用变量参数值

image.png

④在数据体中使用自定义的变量

  1. ### Send request with dynamic variables in request's body
  2. POST https://httpbin.org/post
  3. Content-Type: application/json;charset=UTF-8
  4. {
  5. "id": {{$uuid}},
  6. "price": {{$randomInt}},
  7. "ts": {{$timestamp}},
  8. "value": "content"
  9. }

⑤选择执行HTTP请求的环境

image.png

4、添加Cookie以及Headers

A.在当前请求直接添加

  1. POST {{baseUrl}}customerField/list
  2. Content-Type: application/json;charset=UTF-8
  3. Cookie: Idea-94083848=f69d9e8e-f6c8-42a1-8190-1d91c846dd3a; _ECONTRACT__USER_IDENTITY={}; Webstorm-303cb90f=9c7fe15a-d442-447e-850b-1c00f52bdf55; _ECONTRACT__USER_AVATOR=; _ECONTRACT__TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJnb25neWluZ3NoYW5nIiwiYXV0aCI6IuS8geS4muS_oeaBr-e7tOaKpCzkv67mlLnlr4bnoIEs5YWR5LuY6LSm5oi3566h55CGLOWNj-iurueuoeeQhizljbDnq6DnrqHnkIYs5Y-R56Wo5YiX6KGoLOWPkeelqOeuoeeQhizlt6XkvZzlj7As5byA56uL562-5pS2LOaIkeeahOWNj-iurizmiJHnmoTmlbDlrZflh63or4Es5pON5L2c5ZGY5L-h5oGv566h55CGLOaVsOWtl-WHreivgeWIl-ihqCzmlbDlrZfotYTkuqfono3otYQs5rWB6L2s566h55CGLOa2iOaBr-WIl-ihqCzmtojmga_pgJrnn6Us6J6N6LWE566h55CGLOi0puaIt-euoeeQhizotYTkuqflj5forqks6LWE5Lqn6L2s6K6pLOmqjOelqOiusOW9lSIsInVzZXJfaWQiOjE4MywiZXhwIjoxNTk1NTg2MTAxfQ.tOjiqbdy8ROzweXeM3EeyzrMfH5x-lLqFyk2pWUF3FInxeufl3GfTyKoBiK4kkE2xHgG34bNAarhxb_30SD46w; _ECONTRACT__USER_NAME=%E6%9D%9C%E5%AE%87%E9%98%B3; _ECONTRACT__USER_COMPANY_NAME=undefined; _ECONTRACT__USER_SIGN_STATUS=undefined; _ECONTRACT__USER_ACCESS=SUPPLIER; _ECONTRACT__USER_ID=183; _ECONTRACT__USER_EXECUTIVES=0; _ECONTRACT__USER_PERMISSION=247%2C248%2C252%2C253%2C254%2C257%2C264%2C267%2C275%2C296%2C297%2C299%2C300%2C302%2C303%2C304%2C305%2C306%2C307%2C308%2C329%2C340; _ECONTRACT__USER_LOGIN_NAME=gongyingshang; _ECONTRACT__USER_PERMISSION_CODE=supplier-flow-manager%2Csupplier-open-receipt-manage%2Csupplier-my-digital-assets%2Csupplier-digital-assets-list%2Csupplier-my-digital-certificates%2Csupplier-finance-manage-list%2Csupplier-digital-receive-list%2Csupplier-digital-flow-manage-list%2Csupplier-home%2Csupplier-invoice-manager%2Csupplier-invoice-manager-list%2Csupplier-invoice-inspection-record%2Csupplier-agreement-manager%2Csupplier-account-manager%2Csupplier-account-manager-info%2Csupplier-operator-manager%2Csupplier-account-password%2Csupplier-payment-account-list%2Csupplier-message-manager%2Csupplier-message-list%2Csupplier-sign-agreement-list%2Csupplier-seal-detail; _FINCHAIN__USER_IDENTITY={}; _FINCHAIN__USER_AVATOR=; _FINCHAIN__TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzeXN0ZW0iLCJhdXRoIjoi5LyB5Lia5YiX6KGoLOS_oeaBr-WuoeaguCzlhZHku5jliJ3lrqEs5YWR5LuY5aSN5a6hLOWNj-iuruWIl-ihqCzljY_orq7nrqHnkIYs5Y2w56ug5a6h5qC4LOWNsOeroOeuoeeQhizlt6XkvZzlj7As5bmz5Y-w5Y2P6K6uLOW5s-WPsOi0puWPt-euoeeQhizlvIDnq4vliJ3lrqEs5byA56uL5aSN5a6hLOW8gOeri-mineW6puWkjeaguCzlvIDnq4vpop3luqborr7nva4s5o6I5p2D566h55CGLOaTjeS9nOWRmOS_oeaBr-euoeeQhizmlbDlrZflh63or4HnrqHnkIYs5paw5aKe6LSm5Y-3LOaXpeW_l-WIl-ihqCzml6Xlv5fnrqHnkIYs5raI5oGvLOa2iOaBr-mAmuefpSzoj5zljZXnrqHnkIYs6KeS6Imy566h55CGLOi9rOiuqeWuoeaguCIsInVzZXJfaWQiOjEsImFwcF9pZCI6MSwidXNlcl91dWlkIjoiMjNjd2R2YyIsImV4cCI6MTU5NTkxMzM1MX0.d__DVU-0o2rmW3-tJ4frBI_sOo2JDhmlOn_8t9PdXYTRzsJOfUgJG14jBdkkMQvTf2_FaAGEu95JiLgVmMdTiQ; _FINCHAIN__USER_COMPANY_NAME=undefined; _FINCHAIN__USER_NAME=%E6%8E%A7%E5%88%B6%E5%8F%B0%E7%AE%A1%E7%90%86%E5%91%98; _FINCHAIN__USER_SIGN_STATUS=undefined; _FINCHAIN__USER_ID=1; _FINCHAIN__USER_ACCESS=PLATFORM; _FINCHAIN__USER_EXECUTIVES=1; _FINCHAIN__USER_PERMISSION=24%2C28%2C29%2C220%2C222%2C223%2C230%2C236%2C241%2C242%2C243%2C258%2C259%2C260%2C261%2C268%2C269%2C274%2C314%2C323%2C324%2C325%2C326%2C327%2C342%2C343; _FINCHAIN__USER_LOGIN_NAME=system; _FINCHAIN__USER_PERMISSION_CODE=menu-manager%2Cgrant-manager%2Crole-manager%2Cplatform-company-manager%2Cplatform-company-add%2Cplatform-agreement-manager%2Cplatform-company-list%2Cplatform-agreement-manager-list%2Cplatform-digital-voucher-manage%2Cplatform-digital-open-trial-manage%2Cplatform-digital-open-recheck-manage%2Cplatform-operation-log-manager%2Cplatform-operation-log-list%2Cplatform-message-manager%2Cplatform-message-list%2Cplatform-digital-pay-trial-list%2Cplatform-digital-pay-recheck-list%2Cplatform-home%2Cplatform-operator-manager%09%2Cplatform-digital-check-list%2Cplatform-quota-setting-list%2Cplatform-quota-check-list%2Cplatform-company-info-check%2Cplatform-sign-agreement-list%2Cplatform-seal-detail%2Cplatform-seal-list; Idea-94083849=62fa8c78-3253-4503-8405-0d8dc9b40bff; hasGreet=1; user_avator=; user_access=; user_company_name=[]; USER_EXECUTIVES=; user_permission=; token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJubyBBdXRoZW50aWNhdGlvbiIsInVzZXJfaWQiOjgsImNvbXBhbnlfaWQiOjEsImV4cCI6MTU5OTYxODY3OX0.WV4ONi2bHpYj3tXovsUOrPPAZvC7HyIxK3jcs80tEGhMMe1ye0GR0HQLguxRfdo3wKmxKK6UQPdTae-JX21uGg; user_identity={%22id%22:8%2C%22userNo%22:%226%22%2C%22userName%22:%22admin%22%2C%22password%22:%22$2a$10$kkIIxpqLxyrjebEwnhhvfeW686FUzTEQDaE9RLWL8AaTN8AREXytW%22%2C%22salt%22:%22%E7%9B%90%22%2C%22birthday%22:1514822400000%2C%22name%22:%22%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98%22%2C%22sex%22:1%2C%22certificate%22:%22%22%2C%22idNo%22:%22320581198911011234%22%2C%22wxId%22:%22321321321312321132123%22%2C%22mobNo%22:%2218000000000%22%2C%22email%22:%2211%22%2C%22speciality%22:%221%22%2C%22profession%22:2%2C%22education%22:4%2C%22photo%22:null%2C%22profile%22:%225%22%2C%22identity%22:1%2C%22leaveTime%22:1527651409000%2C%22entryTime%22:1527651464000%2C%22state%22:1%2C%22isDeleted%22:0%2C%22createTime%22:%222018-05-31%2011:52:54%22%2C%22updateTime%22:%222020-07-29%2015:43:09%22%2C%22userResult%22:1%2C%22comId%22:3%2C%22openId%22:%22255554445454%22%2C%22createBy%22:null%2C%22updateBy%22:null%2C%22version%22:null%2C%22status%22:null%2C%22type%22:%22%22%2C%22loginName%22:%22admin%22%2C%22roles%22:null%2C%22roleIds%22:[10]%2C%22permissionList%22:[]%2C%22companyNames%22:null%2C%22roleNames%22:null}; user_name=%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98; user_id=8
  4. {
  5. "type": 1
  6. }

image.png

B.在配置文件创建变量进行引用

①创建私有变量配置文件

image.png

②创建Cookie或者session、Token的私有变量的值

  1. {
  2. "dev": {
  3. "Cookie": "Idea-94083848=f69d9e8e-f6c8-42a1-8190-1d91c846dd3a; _ECONTRACT__USER_IDENTITY={}; Webstorm-303cb90f=9c7fe15a-d442-447e-850b-1c00f52bdf55; _ECONTRACT__USER_AVATOR=; _ECONTRACT__TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJnb25neWluZ3NoYW5nIiwiYXV0aCI6IuS8geS4muS_oeaBr-e7tOaKpCzkv67mlLnlr4bnoIEs5YWR5LuY6LSm5oi3566h55CGLOWNj-iurueuoeeQhizljbDnq6DnrqHnkIYs5Y-R56Wo5YiX6KGoLOWPkeelqOeuoeeQhizlt6XkvZzlj7As5byA56uL562-5pS2LOaIkeeahOWNj-iurizmiJHnmoTmlbDlrZflh63or4Es5pON5L2c5ZGY5L-h5oGv566h55CGLOaVsOWtl-WHreivgeWIl-ihqCzmlbDlrZfotYTkuqfono3otYQs5rWB6L2s566h55CGLOa2iOaBr-WIl-ihqCzmtojmga_pgJrnn6Us6J6N6LWE566h55CGLOi0puaIt-euoeeQhizotYTkuqflj5forqks6LWE5Lqn6L2s6K6pLOmqjOelqOiusOW9lSIsInVzZXJfaWQiOjE4MywiZXhwIjoxNTk1NTg2MTAxfQ.tOjiqbdy8ROzweXeM3EeyzrMfH5x-lLqFyk2pWUF3FInxeufl3GfTyKoBiK4kkE2xHgG34bNAarhxb_30SD46w; _ECONTRACT__USER_NAME=%E6%9D%9C%E5%AE%87%E9%98%B3; _ECONTRACT__USER_COMPANY_NAME=undefined; _ECONTRACT__USER_SIGN_STATUS=undefined; _ECONTRACT__USER_ACCESS=SUPPLIER; _ECONTRACT__USER_ID=183; _ECONTRACT__USER_EXECUTIVES=0; _ECONTRACT__USER_PERMISSION=247%2C248%2C252%2C253%2C254%2C257%2C264%2C267%2C275%2C296%2C297%2C299%2C300%2C302%2C303%2C304%2C305%2C306%2C307%2C308%2C329%2C340; _ECONTRACT__USER_LOGIN_NAME=gongyingshang; _ECONTRACT__USER_PERMISSION_CODE=supplier-flow-manager%2Csupplier-open-receipt-manage%2Csupplier-my-digital-assets%2Csupplier-digital-assets-list%2Csupplier-my-digital-certificates%2Csupplier-finance-manage-list%2Csupplier-digital-receive-list%2Csupplier-digital-flow-manage-list%2Csupplier-home%2Csupplier-invoice-manager%2Csupplier-invoice-manager-list%2Csupplier-invoice-inspection-record%2Csupplier-agreement-manager%2Csupplier-account-manager%2Csupplier-account-manager-info%2Csupplier-operator-manager%2Csupplier-account-password%2Csupplier-payment-account-list%2Csupplier-message-manager%2Csupplier-message-list%2Csupplier-sign-agreement-list%2Csupplier-seal-detail; _FINCHAIN__USER_IDENTITY={}; _FINCHAIN__USER_AVATOR=; _FINCHAIN__TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzeXN0ZW0iLCJhdXRoIjoi5LyB5Lia5YiX6KGoLOS_oeaBr-WuoeaguCzlhZHku5jliJ3lrqEs5YWR5LuY5aSN5a6hLOWNj-iuruWIl-ihqCzljY_orq7nrqHnkIYs5Y2w56ug5a6h5qC4LOWNsOeroOeuoeeQhizlt6XkvZzlj7As5bmz5Y-w5Y2P6K6uLOW5s-WPsOi0puWPt-euoeeQhizlvIDnq4vliJ3lrqEs5byA56uL5aSN5a6hLOW8gOeri-mineW6puWkjeaguCzlvIDnq4vpop3luqborr7nva4s5o6I5p2D566h55CGLOaTjeS9nOWRmOS_oeaBr-euoeeQhizmlbDlrZflh63or4HnrqHnkIYs5paw5aKe6LSm5Y-3LOaXpeW_l-WIl-ihqCzml6Xlv5fnrqHnkIYs5raI5oGvLOa2iOaBr-mAmuefpSzoj5zljZXnrqHnkIYs6KeS6Imy566h55CGLOi9rOiuqeWuoeaguCIsInVzZXJfaWQiOjEsImFwcF9pZCI6MSwidXNlcl91dWlkIjoiMjNjd2R2YyIsImV4cCI6MTU5NTkxMzM1MX0.d__DVU-0o2rmW3-tJ4frBI_sOo2JDhmlOn_8t9PdXYTRzsJOfUgJG14jBdkkMQvTf2_FaAGEu95JiLgVmMdTiQ; _FINCHAIN__USER_COMPANY_NAME=undefined; _FINCHAIN__USER_NAME=%E6%8E%A7%E5%88%B6%E5%8F%B0%E7%AE%A1%E7%90%86%E5%91%98; _FINCHAIN__USER_SIGN_STATUS=undefined; _FINCHAIN__USER_ID=1; _FINCHAIN__USER_ACCESS=PLATFORM; _FINCHAIN__USER_EXECUTIVES=1; _FINCHAIN__USER_PERMISSION=24%2C28%2C29%2C220%2C222%2C223%2C230%2C236%2C241%2C242%2C243%2C258%2C259%2C260%2C261%2C268%2C269%2C274%2C314%2C323%2C324%2C325%2C326%2C327%2C342%2C343; _FINCHAIN__USER_LOGIN_NAME=system; _FINCHAIN__USER_PERMISSION_CODE=menu-manager%2Cgrant-manager%2Crole-manager%2Cplatform-company-manager%2Cplatform-company-add%2Cplatform-agreement-manager%2Cplatform-company-list%2Cplatform-agreement-manager-list%2Cplatform-digital-voucher-manage%2Cplatform-digital-open-trial-manage%2Cplatform-digital-open-recheck-manage%2Cplatform-operation-log-manager%2Cplatform-operation-log-list%2Cplatform-message-manager%2Cplatform-message-list%2Cplatform-digital-pay-trial-list%2Cplatform-digital-pay-recheck-list%2Cplatform-home%2Cplatform-operator-manager%09%2Cplatform-digital-check-list%2Cplatform-quota-setting-list%2Cplatform-quota-check-list%2Cplatform-company-info-check%2Cplatform-sign-agreement-list%2Cplatform-seal-detail%2Cplatform-seal-list; Idea-94083849=62fa8c78-3253-4503-8405-0d8dc9b40bff; hasGreet=1; user_avator=; user_access=; user_company_name=[]; USER_EXECUTIVES=; user_permission=; token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJubyBBdXRoZW50aWNhdGlvbiIsInVzZXJfaWQiOjgsImNvbXBhbnlfaWQiOjEsImV4cCI6MTU5OTYxODY3OX0.WV4ONi2bHpYj3tXovsUOrPPAZvC7HyIxK3jcs80tEGhMMe1ye0GR0HQLguxRfdo3wKmxKK6UQPdTae-JX21uGg; user_identity={%22id%22:8%2C%22userNo%22:%226%22%2C%22userName%22:%22admin%22%2C%22password%22:%22$2a$10$kkIIxpqLxyrjebEwnhhvfeW686FUzTEQDaE9RLWL8AaTN8AREXytW%22%2C%22salt%22:%22%E7%9B%90%22%2C%22birthday%22:1514822400000%2C%22name%22:%22%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98%22%2C%22sex%22:1%2C%22certificate%22:%22%22%2C%22idNo%22:%22320581198911011234%22%2C%22wxId%22:%22321321321312321132123%22%2C%22mobNo%22:%2218000000000%22%2C%22email%22:%2211%22%2C%22speciality%22:%221%22%2C%22profession%22:2%2C%22education%22:4%2C%22photo%22:null%2C%22profile%22:%225%22%2C%22identity%22:1%2C%22leaveTime%22:1527651409000%2C%22entryTime%22:1527651464000%2C%22state%22:1%2C%22isDeleted%22:0%2C%22createTime%22:%222018-05-31%2011:52:54%22%2C%22updateTime%22:%222020-07-29%2015:43:09%22%2C%22userResult%22:1%2C%22comId%22:3%2C%22openId%22:%22255554445454%22%2C%22createBy%22:null%2C%22updateBy%22:null%2C%22version%22:null%2C%22status%22:null%2C%22type%22:%22%22%2C%22loginName%22:%22admin%22%2C%22roles%22:null%2C%22roleIds%22:[10]%2C%22permissionList%22:[]%2C%22companyNames%22:null%2C%22roleNames%22:null}; user_name=%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98; user_id=8"
  4. }
  5. }

③在文件中通过变量值动态引用

  1. POST {{baseUrl}}customerField/list
  2. Content-Type: application/json;charset=UTF-8
  3. Cookie: {{Cookie}}
  4. {
  5. "type": 1
  6. }

image.png

5、设置全局值

例:获取到Token后设置到Client的全局变量,通过请求获取Token将其通过箭头符重定向写入全局设置

  1. POST {{baseUrl}}/authenticate
  2. Content-Type: application/json;charset=UTF-8
  3. {
  4. "username": "admin",
  5. "password": "admin"
  6. }
  7. > {% client.global.set("auth_token", response.body.data) %}

image.png
然后在后续的请求中通过变量引用的方式使用该值即可

  1. POST {{baseUrl}}/profile
  2. Content-Type: application/json;charset=UTF-8
  3. X-ACCESS-TOKEN: {{auth_token}}
  4. {
  5. "userId":"1"
  6. }

6、取消自动重定向操作

只需要在请求上面添加_# _**_@no-redirect_**就行

  1. ### 获取用户信息
  2. # @no-redirect
  3. GET {{bashUrl}}user

6、结果断言

  1. GET {{baseUrl}}/status/200
  2. > {%
  3. client.test("Request executed successfully", function() {
  4. client.assert(response.status === 200, "Response status is not 200");
  5. });
  6. %}