libcurl 的odin语言绑定
基于curl 8.8.0.
- curl静态库 链接
- Windows用
Microsoft Visual Studio 2022 64位.(cl版本19.29.30158)
- 用git克隆当前仓库
- 加
libcurl库到对应系统标识下的foreign import curl {
- 构建windows版本的
libcurl- 使用
win+q打开x64 Native Tools Command Prompt for VS 2019, 或者其他版本如x64 Native Tools Command Prompt for VS 2022等, cd到在curl源码路径下 - 输入
set RTLIBCFG=static - 输入
nmake /f Makefile.vc mode=static, 可以添加别的参数, 参见curl源码路径下winbuild/README.md
- 使用
- 在自己项目中导入当前仓库,
import curl "odin-curl" - (可选)设置log的过程(函数)(
core:log)用来打印一些日志, 默认log不做任何事, 当前仓库使用了log.warnf, 在easyGet和easyPost时指定verbose参数时使用了log.infof - 开始使用, 见下面例子
// 设置日志
logger := log.create_console_logger()
defer log.destroy_console_logger(logger)
context.logger = logger
// 请求结构
Query :: struct {
taskId: string `json:"task_id,omitempty"`,
}
q: Query = {
taskId = "6d50114b-1c13-4cd6-954e-99c5c5385a17",
}
// 请求编码为json
data, _ := json.marshal(q)
// post请求
easy := curl.Easy_post(
"https://xxx/xxx/xxx",
{"Content-Type: application/json"},
body = data,
caPath = "",
verbose = false,
pcap = 2048, // 预先设置容量,默认4096
)
defer curl.Easy_free(easy)
// 解码
res := Res{}
err := json.unmarshal(easy.buf[:], &res)
if err != nil {
fmt.eprintfln("unmarshal failed, err=%v, res=%s", err, res)
return
}
// 处理json解码后的结构数据res- 在
curl_easy_perform时可能会返回错误码23: Failed writing received data to disk/application, 见curl issue, 服务端返回, 看起来不会影响结果