curl interface calling tool

The back-end interface development is completed, are you still waiting for the front-end and unable to debug?

Today I will share a small tool, curl, a command line file transfer tool. It can be used for interface call testing that we usually develop.

It supports file upload and download, supports many protocols including HTTP, HTTPS, ftp, etc., and also supports POST, cookies, authentication and other features.

curl command syntax:

curl [options...]

① Basic usage
curl -X GET/POST

Note that double quotes are required before and after the url here.

@RestController @Slf4j public class CurlController {  @GetMapping("test-get")     public ResultInfo testGet(String name, String age) {         log.info("get params : name {}, age {}", name, age);         return ResultInfo.success();     } }

The request is called as follows:

curl -X GET "http://localhost:12000/server/test-get?name=name1&age=12"

picture

POST:

    @PostMapping("test-post")     public ResultInfo testPost(String name, String age) {         log.info("get params : name {}, age {}", name, age);         return ResultInfo.success("test post response");     }

Request call:

curl -X POST "http://localhost:12000/server/test-post?name=name1&age=12"

picture

②The request body carries parameters
-d 或者 --datacurl -X GET/POST -d "{}"

Note that double quotes are required before and after the data here.

@PostMapping("test-post-with-data")     @ResponseBody     public ResultInfo testPostWithData(@RequestBody User user) {         log.info("get user {}", user);         return ResultInfo.success(user);     }

Request path:

curl -X POST "http://localhost:12000/server/test-post-with-data"-H "Content-Type: application/json" -d "{"name":"postName", "age":23}"

picture

③Carry request header information
-H 或者 --header ,其后可以添加请求头信息,比如 Content-Type、session 等curl -X GET/POST -H ""

For example:

curl -X POST "http://localhost:12000/server/test-post-with-data" -H "Content-Type: application/json"curl -X POST "http://localhost:12000/server/test-post-with-data" -H "session: XXXXXXXXXXXX"

④Carry cookies
-b 或者 --cookie  添加cookie到请求头curl -X POST --cookie "SESSION=XXXXXXXXXXXXXXX"

⑤Download files
-o 或者 --output  下载文件到本地filecurl -X POST "http://localhost:12000/server/test-download" -o "D:/home/test.xls"

@PostMapping("download")     public void download(HttpServletResponse response) throws IOException {         Workbook workbook = new HSSFWorkbook();         Sheet sheet= workbook.createSheet();         Row row = sheet.createRow(0);         Cell cell = row.createCell(0);         cell.setCellValue("test excel");         workbook.write(response.getOutputStream());     }

picture

transfer:

curl -X POST "http://localhost:12000/server/download" -o "D:/home/test.xls"

picture

picture

⑥Upload files
-F 或者 --form<name=content> 上传文件到服务器,服务器端需要用name来接收curl -X POST "http://localhost:12000/server/test-upload" -F "file=@D:/home/test.xls"

Note that you can specify the file name received by the server by specifying filename here :

file=@D:/home/test.xls;filename=myexcel.xls 将本地D:/home/test.xls文件上传到服务器,服务器用file变量接收,file的getOriginalFilename()获取的文件名为myexcel.xls

@PostMapping("upload")     public ResultInfo upload(MultipartFile testFile) throws IOException {         Workbook workbook = new HSSFWorkbook(testFile.getInputStream());         Cell cell = workbook.getSheetAt(0).getRow(0).getCell(0);         return ResultInfo.success("get fileName: " +testFile.getOriginalFilename()                  + ", first cell is << " + cell.getStringCellValue() + ">>");     }

ask:

curl -X POST "http://localhost:12000/server/upload" -F "testFile=@D:/home/test.xls;filename=ttt.xlsx"

picture

The above can basically meet the basic needs. For more commands, please refer to:

curl --help

Guess you like

Origin blog.csdn.net/Jernnifer_mao/article/details/133296115