zoukankan      html  css  js  c++  java
  • LInux命令执行http请求

    Linux下提供了一些命令可以直接执行http请求,下面举例来介绍几个命令。

    http

    // GET请求
    http http://127.0.0.1:2379/version
    http GET http://127.0.0.1:2379/v2/keys/message
    // PUT请求
    http PUT http://127.0.0.1:2379/v2/keys/message value=="hello"
    http PUT http://127.0.0.1:2379/v2/keys/tempkey value=="Gone" ttl==5
    // DELETE请求
    http DELETE http://127.0.0.1:2379/v2/keys/message
    // POST请求
    http POST http://127.0.0.1:8081/api/v1/platform/create name='123' code='123' url='http://www.baidu.com'
    

    curl

    curl请求有许多参数,如下

    -X/--request [GET|POST|PUT|DELETE|…]  指定请求的方式
    -H/--header                           指定请求header
    -i/--include                          显示返回的header
    -d/--data                             指定参数 
    -v/--verbose                          输出更详细信息
    -u/--user                             授权帐号和密码
    -b/--cookie                           cookie 
    

    下面针对每个参数举例说明如何使用

    GET/POST/PUT/DELETE

    curl -X GET "http://www.rest.com/api/users"
    curl -X POST "http://www.rest.com/api/users"
    curl -X PUT "http://www.rest.com/api/users"
    curl -X DELETE "http://www.rest.com/api/users"
    

    HEADER

    curl -v -i -H "Content-Type: application/json" http://www.example.com/users
    

    参数

    // 使用`&`串接多個參數
    curl -X POST -d "param1=value1&param2=value2"
    // 也可使用多個`-d`,效果同上
    curl -X POST -d "param1=value1" -d "param2=value2"
    

    JSON格式资料

    如果需要同时传送request parameter和json,request parameter可以加在url后面,json资料放入-d的参数,header要加入"Content-Type:application/json"

    curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json" -H "Accept:application/json" -d '{"boolean" : false, "foo" : "bar"}'
    
    // 不加"Accept:application/json"也可以
    curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json" -d '{"boolean" : false, "foo" : "bar"}'
    

    需要认证或登录

    这部分主要有两种方式:session和cookie。

    后端如果是session记录使用者登录信息会回传一个session id给前端,前端需要在每次跟后端的requests的headers中置入此session id,后端便会以此session id识别前端是属于哪个session。

    curl --request GET 'http://www.rest.com/api/users' --header 'sessionid:1234567890987654321'
    

    如果是使用cookie,在登录后,后端会回一个cookie回来,把该cookie存档,当下次请求时,再用-b cookie_file的方式在request中植入cookie即可。

    // 將cookie存檔
    curl -i -X POST -d username=kent -d password=kent123 -c  ~/cookie.txt  http://www.rest.com/auth
    // 載入cookie到request中 
    curl -i --header "Accept:application/json" -X GET -b ~/cookie.txt http://www.rest.com/users/1
    

    Authentication

    如果网站采用了HTTP基本认证,可以使用--user username:password进行授权

    curl -i --user suraer:secret "http://www.rest.com/api/foo"
    

    参考链接: http://ju.outofmemory.cn/entry/84875

    wget

    和curl相似的,如果url路径指向一个文件或者图片的话,可以直接下载该文件或者图片。
    这里只距离说明发送get和post请求的简单使用,具体内容可以查看相关资料。

    // get请求
    wget "http://www.baidu.com"
    // post请求
    wget --post-data 'user=foo&password=bar' http://www.baidu.com
    
  • 相关阅读:
    sp2010 升级sp2013 用户无法打开网站
    powerviot install in sharepoint 2013
    can not connect cube in performancce dashboard
    westrac server security configure user info
    添加报表服务在多服务器场
    sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
    delete job definition
    目前付款申请单内网打开慢的问题
    item style edit in sharepoint 2013
    Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
  • 原文地址:https://www.cnblogs.com/suraer/p/8625657.html
Copyright © 2011-2022 走看看