You cannot just pass the os.File
like that. You need to actually read the file, construct MIME multi-part request body (see spec) and send it in POST request.
buf := &bytes.Buffer{}
w := multipart.NewWriter(...)
// add other required fields (operations, map) here
// load file (you can do these directly I am emphasizing them
// as variables so code below is more understandable
fileKey := "0" // file key in 'map'
fileName := "file.xslx" // file name
fileContentType := "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
fileContents, err := ioutil.ReadFile("./file.xlsx")
// ...
// make multipart body
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fileKey, fileName))
h.Set("Content-Type", fileContentType)
ff, err := bodyWriter.CreatePart(h)
// ...
_, err = ff.Write(fileContents)
// ...
err = bodyWriter.Close()
// ...
req, err := http.NewRequest("POST", fmt.Sprintf("https://endpoint"), buf)
//...
There is a good working example of doing this in gqlgen
repository itself here: example/fileupload/fileupload_test.go.
In that example each file is loaded into (and represented by) file
struct type defined on line I linked which might make it a bit confusing at first sight.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…