今天測試的時候mock一個站點的訪問,使用curl調用一直報400,Bad Request,缺少參數。
提示缺少參數,明明加了啊,參數使用&連接,仔細觀察命令沒有結束。因為linux中 &代表命令在后台執行,因此命令沒有結束。
使用wireshark抓包分析
可以看到確實沒有documentIds參數
二種解決方案:
1. 使用"" 把請求體包起來: curl -i "http://localhost/v2/***"
2. 使用\轉義 curl -i http://localhost/v2/65/appid=4\&documentIds=136,137
說明:
curl post請求:
curl -H "Content-Type:application/json" -X POST -d '{"user": "admin", "passwd":"12345678"}' http://127.0.0.1:8000/login
附上為了mock接口寫的代碼
@RestController public class IDImgsController { @RequestMapping("/v2/DocumentService/getBatchDocuments/{id}") public String getImgs(@PathVariable Integer id, @RequestParam("appid") String appid, @RequestParam("documentIds") String documentIds){ return "{\n" + " \"data\": [\n" + " {\n" + " \"creation_date\": \"2018-09-07 14:24:04\",\n" + " \"document_id\": 136,\n" + " \"file_name\": \"二代身份證正面\",\n" + " \"front_document_id\": 68,\n" + " \"url\": \"http://f.ppdai.com/**.jpg\",\n" + " \"user_id\": 8487008\n" + " },\n" + " {\n" + " \"creation_date\": \"2018-09-07 14:24:15\",\n" + " \"document_id\": 137,\n" + " \"file_name\": \"二代身份證反面\",\n" + " \"front_document_id\": 69,\n" + " \"url\": \"http://file11info.ppdai.com/***.jpg\",\n" + " \"user_id\": 8487008\n" + " },\n" + " {\n" + " \"creation_date\": \"2018-09-07 14:24:31\",\n" + " \"document_id\": 137,\n" + " \"file_name\": \"二代身份證反面\",\n" + " \"front_document_id\": 69,\n" + " \"url\": \"http://file11info.ppdai.com/***.jpg\",\n" + " \"user_id\": 8487008\n" + " },\n" + " {\n" + " \"creation_date\": \"2018-09-07 14:24:54\",\n" + " \"document_id\": 138,\n" + " \"file_name\": \"本人手持二代身份證的合照\",\n" + " \"front_document_id\": 70,\n" + " \"url\": \"http://file11info.ppdai.com/***.jpg\",\n" + " \"user_id\": 8487008\n" + " }\n" + " ],\n" + " \"msg\": \"success\",\n" + " \"ret\": 0,\n" + " \"serial_no\": \"747a095e-f80e-4879-a288-f75e1e4bd73b\"\n" + "}"; } }