Post with file location

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Post with file location



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.


d



Any tips on how to solve it?



Thanks!





You'll have to open the file using os.File and send it via the request body.
– Adrian
Aug 10 at 14:35


os.File




1 Answer
1



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.


ioutils


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS