Issue with net/http

I am testing an API and there is a url to upload files, say data/import.
The contentType is multipart/form but only the file is sent. }
I have the following code for an http client

     req, _ := http.NewRequest(http.MethodPost, "/data/import", ?)

I don’t know what to put in the “?”.
Will it be the path of the file directly or do I have to read the file with a reader
and pass the bytes slide?

TIA,
Yamil

NewRequest takes an io.Reader:

func NewRequest(method, url string, body io.Reader) (*Request, error)

So, for testing purposes you could read from a string or something similar:

r := strings.NewReader("Test Data")
req, _ := http.NewRequest(http.MethodPost, "/data/import", r)

You could also read from a file if you wish with something along the lines of this:

file, err := os.Open("testdata.txt") // For read access.
if err != nil {
	// Do something with the error
	log.Fatal(err)
}
defer file.Close()
req, _ := http.NewRequest(http.MethodPost, "/data/import", file)

Totally wrote this code without compiling it or anything so you might need to tweak it to get it to actually work, but that should get you started!

Thanks a lot!!!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.