同时下载多个文件 curl -o save.txt http://localhost:8080/simple-service-webapp/test/hello -o save2.txt http://localhost:8080/simple-service-webapp/test/hello2
链接重定向 使用 -L 跟随链接重定向,逻辑为http://codebelief.com -> http://www.codebelief.com 实例:curl -L http://codebelief.com使用 -H 自定义 header 实例1:curl -H “Referer: www.example.com” -H “User-Agent: Custom-User-Agent” http://www.baidu.com 实例2:curl -H “Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com使用 -c 保存 Cookie 当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。-c 后面跟上要保存的文件名。 实例:curl -c “cookie-example” http://www.example.com使用 -b 读取 Cookie 前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下: 实例1:curl -b “JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com 实例2:curl -b "cookie-example " http://www.example.com使用 -d 发送 POST 请求 我们以登陆网页为例来进行说明使用 cURL 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 CURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式: 实例1:curl -d “userName=tom&passwd=123456” -X POST http://www.example.com/login 实例2:curl -d " {“name”:“1234”,“id”:“c62f7bff-d8d7-402a-935f-e33b539a9ed7”}" -X POST http://www.example.com/login 参数以文件方式 实例3:curl -d “@data.txt” -X POST http://www.example.com/login文件上传 使用了-F参数,curl会以multipart/form-data的方式发送POST请求。-F以key=value的形式指定要上传的参数,如果是文件,则需要使用key=@file的形式。 实例1:curl -F “key=value” -F “filename=@file.tar.gz” http://localhost/upload其他命令 其他命令参考:http://man.linuxde.net/curlCurl命令:
curl -i -H “Content-Type:application/json;charset=UTF-8” -H “X-Auth-Token:1234567876543” http://localhost:8080/simple-service-we bapp/demo/doGet?name=1234
返回结果
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json Content-Length: 151 Date: Sun, 26 May 2019 13:26:50 GMT {"datas":{"name":"1234","id":"e899d723-e140-4d32-a59a-23fff09a82cf"},"header":{"contentType":"application/json;charset=UTF-8","token":"1234567876543"}} POST方式 后台逻辑: @POST @Produces(MediaType.APPLICATION_JSON) @Path("/doPost") public Response doPost(String param, @Context HttpHeaders httpHeader, @Context UriInfo uriInfo) { // 请求头 String contentType = httpHeader.getHeaderString("Content-Type"); String token = httpHeader.getHeaderString("X-Auth-Token"); JSONObject headerObject = new JSONObject(); headerObject.put("contentType", contentType); headerObject.put("token", token); JSONObject dataObject = JSONObject.parseObject(param); JSONObject resultObject = new JSONObject(); resultObject.put("datas", dataObject); resultObject.put("header", headerObject); return Response.ok(resultObject, MediaType.APPLICATION_JSON).status(Response.Status.OK).build(); }Curl命令:
curl -i -H “Content-Type:application/json;charset=UTF-8” -H “X-Auth-Token:1234567876543” -d “{“vimId”:“vim3”,“regionId”:“Reg ionOne”,“vnfrId”:“vnf1”,“operationId”:“8c500087-81bd-42fe-b227-28a3094f9f7e”,“changeType”:“scale-in”,“serverList”:[“b44a03f8-ec02-48a9-91da-cbe064b8d99b”,“bee3803b-509c-45e6-b278-43cc3614886b”,“cc3ea4de-65a4-434a-b7da-6ceb26cea1dc”],“callType”:“0”,“realEnd”:“false”}” http://localhost:8080/simple-service-webapp/demo/doPost
返回结果
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json Content-Length: 390 Date: Sun, 26 May 2019 13:30:49 GMT {"datas":{"vimId":"vim3","regionId":"Reg ionOne","vnfrId":"vnf1","realEnd":"false","changeType":"scale-in","serverList":["b44a03f8-ec02-48a9-91da-cbe064b8d99b","bee3803b-509c-45e6-b278-43cc3614886b","cc3ea4de-65a4-434a-b7da-6ceb26cea1dc"],"operationId":"8c500087-81bd-42fe-b227-28a3094f9f7e","callType":"0"},"header":{"contentType":"application/json;charset=UTF-8","token":"1234567876543"}}