<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] 搭建restful API很簡單,引入**cors中間件**即可,不需要設置請求頭為**Access-Control-Allow-Origin**,這個中間件會自動幫我們設置。 # 演示代碼 ```javascript const Koa = require('koa'); const logger = require('koa-logger'); const Router = require('koa-router'); const cors = require('@koa/cors'); const app = new Koa(); //記錄日志 app.use(logger()); //支持跨域請求 app.use(cors()); // 主頁 let routerHome = new Router(); routerHome.get('/', async (ctx, next) => { ctx.body = '歡迎歡迎!'; }) // restful api let routerRest = new Router(); routerRest.get('/resource', async (ctx, next) => { ctx.body = { errno: 0, errmsg: 'GET API執行成功', data: '返回的數據' }; }).post('/resource', async (ctx, next) => { ctx.body = { errno: 0, errmsg: 'POST API執行成功', data: '返回的數據' }; }) // 裝載所有路由 let router = new Router(); router.use('/', routerHome.routes(), routerHome.allowedMethods()); router.use('/restful', routerRest.routes(), routerRest.allowedMethods()); app.use(router.routes(), router.allowedMethods()); //監聽3000端口 app.listen(3000, () => { console.log('server is running at http://localhost:3000') }); ``` # 測試 > 使用curl命令測試RESTful API ## GET請求 ``` $curl http://localhost:3000/restful/resource ``` ## POST請求 ``` $curl http://localhost:3000/restful/resource -X POST -H "Content-Type:application/json" -d '{"a":"abc","b":101}' ``` > 可以在瀏覽器中裝一個RESTful插件發送請求測試 ![](https://box.kancloud.cn/4798a2ee4a77d38b67d7bb54eaeb56d4_1110x564.png) # 使用curl命令測試RESTful API Representational State Transfer,簡稱REST 是一種全新的軟件架構風格、設計風格(特別在http web服務領域)。 curl是利用URL語法在命令行方式下工作的開源文件傳輸工具。它被廣泛應用在Unix、多種Linux發行版中。 **curl基本語法** ``` curl [option] [url] ``` 可選的option有很多,可通過 curl --help 查看,重點介紹下幾個常用的選項。? -X? ? 或者 --request? ? 指定請求方式,如GET 、POST 、PUT 、DELETE 、HEAD 等七種方式 -i? ?或者? --include? ? 顯示服務器response 響應頭部信息 -v? 或者? --verbose? ? 顯示詳細(冗長)信息 -H? 或者? --header? 指定http 請求頭 ,如? -H? "Content-Type:application/json"? -d? 或者? --data? 指定請求體body參數 , 如有多個參數可以用&隔開或者使用多個-d 選項。 如? -d "a=abc&b=110&c=true" (指定三個參數)或? -d a=abc -d b=110 -d c=true 亦可。 -F? 或者? --form? 指定multipart Form 請求體,如文件域參數,或者普通表單域參數。 -u 或者? --user? 指定用戶名:密碼? -C? 或者? --continue-at? offset? 用于斷點續傳。 -c? ?或者 --cookie-jar? ?請求返回回寫cookie到指定文件中 -D? 或者? --dump-header? 請求返回回寫response header信息到指定文件中 -b? 或者? --cookie? 請求時攜帶上cookie,指定本地cookie所在文件? -x? 或者? --proxy? ?指定 http代理服務器ip:port? -O? 請求Url 并保存到本地 -o? 請求Url并保存到指定文件 1、直接請求 ? curl? ? http://www.tingcream.com? ? #默認? 使用get 請求方式,content-type為form-unlencoded?? ? curl? ?-i? ? http://www.tingcream.com? ?# -i 顯示response head信息 2 、下載url資源(文件、圖片)到本地 下載圖片到本地,圖片名稱為服務器默認確定的名稱 ``` $?curl -O? ?http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png? ? ``` 下載圖片到本地并指定文件名稱 ``` $curl? -o 127.png http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png? ``` 3 、請求時指定請求方式 ``` $curl? ? www.tingcream.com? -X POST? ``` > -X POST或者 -X post?,-X PUT 或者 -X put? ? # 使用post、put方式請求服務 4 、使用查詢字符串傳參(get) ``` ? ?curl? http://xxx.com?username=zhangsan&pwd=123?? ``` 5 、使用post body傳參數 ``` ? ?curl? -v http://xxxx.com? -X POST -d username=zhangsan -d password=123456? ``` 或者 ``` ? ?curl -v? http://xxxx.com? -X POST -d? 'username=zhangsan&password=123456'?? ``` 6 、使用multipart Form 傳參(文件上傳) ? ?curl? ?http://xxx.com? ?-X POST? -F "myFile=@/usr/local/aaa.txt" (文件參數)? -F "username=zhangsan" (普通文本參數) 7、cookie的回寫與使用 ? ? curl -c? ~/cookie.txt? ?http://xxx.com? ?#請求回寫cookie到本地文件 ? ? curl -b? ~/cookie.txt? http://xxx.com? ?#再次請求,攜帶上cookie (sesisonId) 8、使用http 代理服務器訪問url ? ?curl? ?-x? proxyIP:proxyPort? http://xxx.com?? 9 、使用-C? 進行斷點續傳? 11 、restFull API?使用Post JSON ``` $curl? http://xxx.com? ?-X POST -H "Content-Type:application/json"? -d? '{"a":"abc","b":101}'? ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看