Anthony Cooper
Anthony Cooper

Reputation: 465

golang server: how to retrieve multiple files continuously

I have implemented a http server based on gin (golang web framework).

I post 2 pictures to server with curl multipart/form-data:

curl -X POST -F upload0=@jpg -F upload1=@jpg -H "Content-Type: multipart/form-data" "http://server:port/path"

Server code looks like this:

func extractImgs(c *gin.Context) {
    prefix := "prefix"
    for ix := 0; ix < 2; ix++ {
        file, _, err := c.Request.FormFile(prefix + strconv.Itoa(ix))
        if err != nil {
            // do sth.
            return
        }
        face_recognize_async(file)
    }
}

You know, face recognition is time-consuming, I hope the work-flow is:

get_1st_img -> recognize_face -> get_2nd_img -> recognize_face -> ...

I print c.Request.FormFile() execution time, it returns after retrieved all 2 files.

My questions:

1) how to retrieve these files continuously, just like traverse linked list;

2) Is http multipart a good choice, should I implement it with TCP/STCP?

Upvotes: 0

Views: 1275

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109378

Since FormFile indexes the files from the posted form, it requires that the entire form already be parsed. From the FormFile docs:

FormFile calls ParseMultipartForm and ParseForm if necessary.

If you want to stream the the multipart form one part at a time, use Request.MultipartReader

Upvotes: 1

Related Questions