1. 程式人生 > >Linux用curl呼叫應用介面

Linux用curl呼叫應用介面

很多同學都用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<String, User> map = new HashMap<String,User>();
        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

[[email protected]kin-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"}[
[email protected]
~]# [[email protected] ~]#

  通過測試可以發現報400錯誤,原因現在沒有指定request body引數,下面先通過指定一個空json資料測試

2.2.測試二:

curl -i -X POST --data '{}' http://192.168.20.149:8080/testc/1

[[email protected] ~]# 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

[[email protected] ~]# 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}' \

[[email protected] ~]# 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
[[email protected] ~]# 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}' \

[[email protected] ~]# 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}' \

[[email protected] ~]# 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}' \

[[email protected] ~]# 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}' \

[email protected] 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}' \

[email protected] 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}}

  可以發現測試完美通過了,這下終於可以洗洗睡啦~~