发布文件位置

I am trying to send this request through a POST using Go.

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'

At this moment I can't figure out how to deal with the -F parameter.

I've created the following struct

type DocumentRequest struct {
    Type string `json:"type"`
    File string `json:"file"`
}

which I am sending through:

res, err := s.Post(assembleURL(“https://api.onfido.com/v2/applicants/", userID, "documents"), d, doc, &apiErr)

where d is my DocumentRequest.

Any tips on how to solve it?

Thanks!

You're gonna have to read the file and add it as the request body. There's a helper in ioutils and you can just get rid of your struct and pass the body directly. This is the gist of it. Ofc, you should handle the err's and you might need to prepare your req up front so you can add additional headers before sending.

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