http_requests
简介
发送 http 请求的库
- 支持 GET, POST, PUT 三种形式的 http 请求
- 支持 http 以及 https 请求, 其中 https 通过 wolfssl 管理, 使用 TLS1.2 规范
使用建议
- 使用时不要对 url 进行转义, pkg 内部会进行转义, 不然会发生多重转义
- 尽量使用 POST 不要使用 GET
- 在对延时要求高的场景, 尽量使用 http 不要使用 https
与 httpclient 的对比
优点
- 没有使用 object 对连接进行管理, 单次连接效率更高
- 反回的数据更加全面, httpclient 将许多返回值隐藏, 该 pkg 将全部值返回
缺点
- 指定 Cotent-type 需要通过字符串指定, 不太方便
- 不支持长连接
组件接口
http_requests.gs
函数原型 | 函数作用 |
---|---|
map request(string cmd, string url, mixed data = nil, map opts = nil, int timeout = 3) | 发送 http 请求, 默认短链接, 不推荐直接调用, 建议使用 get 或者 post 间接调用 |
map get(string url, map opts = nil, int timeout = 3) | 以 get 的方式发送 http 请求 |
map post(string url, mixed data, map opts = nil, int timeout = 3) | 以 post 的方式发送 http 请求 |
map put(string url, mixed data, map opts = nil, int timeout = 3) | 以 put 的方式发送 http 请求 |
示例
import pkg.http_requests;
// begin @http_requests.post
// 简单使用
map res_post = http_requests.post("https://www.bing.com", nil);
write(res_post);
// end
// begin @http_requests.post
// 使用 json 发送数据
map res_post2 = http_requests.post("http://localhost:8888/test", {"data": "123"}, {"Content-Type": "application/json; charset=UTF-8"});
write(res_post2);
// end
// begin @http_requests.get
map res_get = http_requests.get("https://www.baidu.com/s?wd=g-bits");
write(res_get);
// end