首页 > 解决方案 > 发布文件位置

问题描述

我正在尝试使用 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在哪里。

关于如何解决它的任何提示?

谢谢!

标签: curlgopost

解决方案


您将不得不阅读该文件并将其添加为请求正文。有一个助手ioutils,你可以摆脱你的结构并直接传递身体。这是它的要点。Ofc,您应该处理错误,并且您可能需要预先准备好您的请求,以便您可以在发送之前添加额外的标头。

body, err := ioutils.ReadFile(yourPath)
reqs, err := http.Post(uri, "image/png", &body)

推荐阅读