很多同学都用postman测试过后台应用接口,的确非常方便。今天这里介绍一下在Liunx下用curl命令测试接口的方法和遇到的一些问题,包括url传参转义和中文传参报错的问题,这里都提供解决办法。下面我们来看一个示例吧:
1.我们的后台采用JAVA程序接口,这里只列部分代码
@PostMapping("/testc/{id}")
public Object testc(@PathVariable("id") Long id,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "age", required = false) Integer age,
@RequestBody User user){
Map map = new HashMap();
User u = new User();
u.setId(id);
u.setName(name);
u.setAge(age);
map.put("uuu", u);
map.put("uuu2", user);
return map;
}
2.启动应用后(接口地址:http://192.168.20.149:8080/testc),我们在shell命令行中进行测试(如果是windows上可以在GitBash上进行测试)
2.1.测试一:
curl -i -X POST http://192.168.20.149:8080/testc/1
[root@chinoukin-n1 ~]# curl -i -X POST http://192.168.20.149:8080/testc/1
HTTP/1.1 400
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 06:46:12 GMT
Connection: close
{"timestamp":1537425972603,"status":400,"error":"Bad Request","message":"Required request body is missing: public java.lang.Object com.wisea.demoh2.controller.TestController.testc(java.lang.Long,java.lang.String,java.lang.Integer,com.wisea.demoh2.entity.User)","path":"/testc/1"}[root@chinoukin-n1 ~]#
[root@chinoukin-n1 ~]#
通过测试可以发现报400错误,原因现在没有指定request body参数,下面先通过指定一个空json数据测试
2.2.测试二:
curl -i -X POST --data '{}' http://192.168.20.149:8080/testc/1
[root@chinoukin-n1 ~]# curl -i -X POST --data '{}' http://192.168.20.149:8080/testc/1
HTTP/1.1 415
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 06:52:12 GMT
{"timestamp":1537426332587,"status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/testc/1"}
通过测试可以发现报415错误,原因是请求头参数Content-Type没有指定,默认是application/x-www-form-urlencoded;charset=UTF-8,下面我们指定为application/json来测试
2.3.测试三:
curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{}' http://192.168.20.149:8080/testc/1
[root@chinoukin-n1 ~]# curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{}' http://192.168.20.149:8080/testc/1
HTTP/1.1 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:02:22 GMT
{"uuu":{"id":1,"name":null,"age":null},"uuu2":{"id":null,"name":null,"age":null}}
可以发现通过测试了,下面补全参数进行测试
2.4.测试四:
curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{"id":2,"name":"chenyingqin","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin&age=19
[root@chinoukin-n1 ~]# curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{"id":2,"name":"chenyingqin","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin&age=19
[1] 2463
[root@chinoukin-n1 ~]# HTTP/1.1 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:06:24 GMT
{"uuu":{"id":1,"name":"chinoukin","age":null},"uuu2":{"id":2,"name":"chenyingqin","age":18}}
可以发现测试通过了,但是可以看到对象“uuu”的age是空,而设定的是19。通过第二行不难发现,这是因为Linux把URL传参中的“&”当成启动后台进程了,下面进行字符转义(\&)测试
2.5.测试五:
curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{"id":2,"name":"chenyingqin","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin\&age=19
[root@chinoukin-n1 ~]# curl -i -X POST -H 'Content-Type:application/json;charset=UTF-8' --data '{"id":2,"name":"chenyingqin","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin\&age=19
HTTP/1.1 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:13:55 GMT
{"uuu":{"id":1,"name":"chinoukin","age":19},"uuu2":{"id":2,"name":"chenyingqin","age":18}}
可以发现测试完美通过,对象“uuu”的age也正常了。这是同学们可能觉得就完美了,已经完事了,洗洗睡了。但是...
2.6.测试六:
curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin大侠\&age=19
[root@chinoukin-n1 ~]# curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin大侠\&age=19
HTTP/1.1 400
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:23:25 GMT
Connection: close
通过测试可以发现报400错误,原因是URL传参中含有中文,下面通过UrlEncode转码一下再进行测试
2.7.测试七:
curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
[root@chinoukin-n1 ~]# curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
HTTP/1.1 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:28:19 GMT
{"uuu":{"id":1,"name":"chinoukin大侠","age":19},"uuu2":{"id":2,"name":"chenyingqin大侠","age":18}}
可以发现测试完美通过,这时同学们可能觉得终于可以洗洗睡啦,但是...,当我在GitBash上测试,结果不尽人意
2.8.测试八(GitBash):
curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
chinoukin@DESKTOP-PH67EF6 MINGW64 ~/Desktop
$ curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin大侠","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 526 0 484 100 42 32266 2800 --:--:-- --:--:-- --:--:-- 35066HTTP/1.1 400
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:33:09 GMT
Connection: close
{"timestamp":1537428789546,"status":400,"error":"Bad Request","message":"JSON parse error: Invalid UTF-8 start byte 0xb4\n at [Source: (PushbackInputStream); line: 1, column: 29]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xb4\n at [Source: (PushbackInputStream); line: 1, column: 29]\n at [Source: (PushbackInputStream); line: 1, column: 16] (through reference chain: com.wisea.demoh2.entity.User[\"name\"])","path":"/testc/1"}
通过测试可以发现报400错误,原因是windows系统默认编码格式是GBK,而命令行中的中文参数在提交时被转码成GBK了,下面将中文字符进行Unicode转码后再进行测试
2.9.测试九(GitBash):
curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin\u5927\u4fa0","age":18}' \
http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
chinoukin@DESKTOP-PH67EF6 MINGW64 ~/Desktop
$ curl -i -X POST -H 'Content-Type:application/json' --data '{"id":2,"name":"chenyingqin\u5927\u4fa0","age":18}' http://192.168.20.149:8080/testc/1?name=chinoukin%e5%a4%a7%e4%be%a0\&age=19
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 152 0 102 100 50 99k 50000 --:--:-- --:--:-- --:--:-- 148kHTTP/1.1 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Sep 2018 07:37:27 GMT
{"uuu":{"id":1,"name":"chinoukin大侠","age":19},"uuu2":{"id":2,"name":"chenyingqin大侠","age":18}}
可以发现测试完美通过了,这下终于可以洗洗睡啦~~