curl - 发布文件位置
问题描述
我正在尝试使用 Go 通过 POST 发送此请求。
curl https://api.onfido.com/v2/applicants/1030303-123123-123123/documents \
-H 'Authorization: Token token=your_api_token' \
-F 'type=passport' \
-F 'file=@localfile.png;type=image/png'
目前我无法弄清楚如何处理 -F 参数。
我创建了以下结构
type DocumentRequest struct {
Type string `json:"type"`
File string `json:"file"`
}
我正在发送:
res, err := s.Post(assembleURL(“https://api.onfido.com/v2/applicants/", userID, "documents"), d, doc, &apiErr)
d
我的 DocumentRequest在哪里。
关于如何解决它的任何提示?
谢谢!
解决方案
您将不得不阅读该文件并将其添加为请求正文。有一个助手ioutils
,你可以摆脱你的结构并直接传递身体。这是它的要点。Ofc,您应该处理错误,并且您可能需要预先准备好您的请求,以便您可以在发送之前添加额外的标头。
body, err := ioutils.ReadFile(yourPath)
reqs, err := http.Post(uri, "image/png", &body)
推荐阅读
- c# - 使用通道工厂将身份验证令牌添加到 WCF 服务调用中
- typo3 - 服务不可用 (503) 找不到 TypoScript 模板
- java - JPA @Formula 没有架构名称或使用实体类进行配置
- python - 如何以随机顺序执行多个功能?
- queue - 如何为 Slurm 作业指定依赖项?
- c++ - C++20协程可以复制吗?
- kubernetes - 为创建的声明修改持久卷名
- javascript - JavaScript 空字符串检查效率
- php - 使用 PHP 和 jquery 导出到 Excel 时,数据不显示在列中
- android - NavigationView 中的分隔线(在 BottomSheetDialogFragment 中)未显示?