2025年4月8日 星期二 乙巳(蛇)年 正月初九 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 网络服务

IDEA HTTP Request 真香

时间:12-14来源:作者:点击数:7
CDSY,CDSY.XYZ

领导想 POSTMAN 建个组,这样就能共享请求了,结果只能加5个,超了得交钱,我说,何不 IDEA HTTP Request 呢,据说很香。

一些例子

GET

  • GET http://localhost:8090/persons/all

HTTP header, you can include them with the other headers

  • GET http://localhost:8090/persons/all
  • Accept-Language:en-US,en;q=0.9
  • Authorization:BasicQWxhZGRpbjpvcGVuIHNlc2FtZQ==
  • Cookie: name=value; name2=value2; name3=value3

Adding body

  • POST http://localhost:8090/persons/add/
  • Content-Type: application/json
  • {
  • "name": "JohnDoe",
  • "age": 29
  • }

File upload

  • POST http://localhost:8090/persons/add/
  • Content-Type: application/json
  • < person.json

Environmental variables

rest-client.env.json

  • {
  • "local":{
  • "host":"localhost"
  • },
  • "development":{
  • "host":"dev.example.com"
  • },
  • "production":{
  • "host":"example.com"
  • }
  • }
  • GET http://{{host}}:8090/persons/all/

Live templates

Template Usage
gtr GET Request
gtrp GET Request with query parameters
ptr POST Request with simple body
ptrp Post Request with parameter-like body
mptr Post Request to submit a form
fptr Post Request to submit a form with a file

Testing Responses

可以使用 client and response.

  • GET https://www.vojtechruzicka.com/
  • > {%
  • client.test("Request executed successfully", function() {
  • client.assert(response.status === 200, "Response status is not 200");
  • });
  • %}
  • GET https://www.vojtechruzicka.com/
  • > {%
  • client.test("Request executed successfully", function() {
  • client.log("Look, I can log!");
  • client.assert(response.status === 200, "Response status is not 200");
  • client.assert(response.contentType.charset === "UTF-8", "Unexpected content type - charset");
  • client.assert(response.contentType.mimeType === "text/html", "Unexpected content type - MIME type");
  • client.assert(response.headers.valueOf("x-frame-options") === "DENY", "Frame options not set to DENY");
  • client.assert(response.body.indexOf("Vojtech") !== -1, "Expected body to contain 'Vojtech'");
  • });
  • %}

Preserving data between requests

  • GET https://www.example.com/auth
  • Content-Type: application/json
  • {
  • "token": "secret-token"
  • }
  • > {%
  • client.global.set("token", response.body.json.token);
  • %}
  • GET https://www.example.com/foo
  • Authorization: Bearer {{token}}

Spring Boot Integration

Endpoints → Mappings

Converting cURL requests

Since version 2019.2, you can easily convert your existing cURL requests to the format supported by IntelliJ IDEA. All you need to do is to paste a cURL reuest like this to the editor of Intellij's HTTP client:

curl -X POST http://www.example.com/login/ -d 'username=joe&password=1234'

It will be automatically converted to the format supported by IDEA:

  • POST http://www.example.com/login/
  • Content-Type: application/x-www-form-urlencoded
  • username=joe&password=1234

Pretty cool, right? Alternatively you can go through the menu:

Tools → HTTP Client → Convert cURL to HTTP Request

HTTP Proxy

In case you need HTTP proxy for your calls, both clients share proxy settings of the whole IDE, which can be configured under Appearance & Behavior → System Settings → HTTP Proxy.

History

What's cool is that IDEA automatically stores the history of all the requests you performed (up to 50 items). It is available from Tools → HTTP Client → Show HTTP Requests history. What it actually does is that it opens a file called http-requests-log.http in .idea/httpRequests folder. It can look something like this:

  • POST http://localhost:8090/persons/add
  • Content-Type: application/json
  • < C:/_MY/rest-demo/person.json
  • <> 2018-01-11T032255.200.txt
  • GET http://localhost:8090/persons/all
  • <> 2018-01-11T115612.200.json
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐