💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[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}'  ```