vrmiguel
(Vinicius Rodrigues)
July 18, 2020, 7:57am
1
Hello there!
I’m trying to make a POST request equivalent to curl -d "file=@filename" https://xxx/upload
I tried the following:
formData := url.Values{
"file": {"@" + filename},
}
//req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))
resp, err := http.PostForm("https://api.anonfiles.com/upload", formData)
I believe I should use CreateFormFile
but its documentation is somewhat confusing.
Any help would be appreciated!
skillian
(Sean Killian)
July 19, 2020, 11:36am
2
Hi, Vinicius,
That “file=@filename” notation is specific to curl. In Go, you might need to handle reading the data yourself. I think this will do it (or at least, get you started):
func POSTFile(filename string) (*http.Response, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return POSTData(f)
}
func POSTData(r io.Reader) (*http.Response, error) {
sb := strings.Builder{}
if _, err := io.Copy(sb, r); err != nil {
return nil, err
}
formData := url.Values{
"file": []string{sb.String()},
}
return http.PostForm("https://api.anonfiles.com/upload", formData)
}
Note that this will only work if your file is text, but I think that matches curl’s -d, --data
parameter’s usage.
2 Likes
vrmiguel
(Vinicius Rodrigues)
July 19, 2020, 11:45pm
3
Hello, Sean! Thanks a lot for your response.
I forgot to update this post but I eventually solved the problem with a method similar to the one you’ve shown:
mpb := bytes.NewBuffer(nil)
mw := multipart.NewWriter(mpb)
partWriter, err := mw.CreateFormFile("file", filename)
checkErr(err)
fileReader, err := os.Open(filename)
checkErr(err)
io.Copy(partWriter, fileReader)
mw.Close()
However, I still have a question about this. Does io.Copy
actually copy the file’s content to the computer’s RAM? If it does, could there be a better way to upload large files?
Thanks again for your time!
NobbZ
(Norbert Melzer)
July 20, 2020, 6:57am
4
This depends on the implementations of the involved buffers.
Though implementations usually strive to be implemented in a way that they don’t need to hold full content in memory, unless reading/writing directly from/to memory.
system
(system)
Closed
October 18, 2020, 6:57am
5
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.