resty icon indicating copy to clipboard operation
resty copied to clipboard

Multiple files posting with same key

Open avinash92c opened this issue 5 years ago • 7 comments

If you check in postman, you can see that u can send an array of under one key in form body. Using the same thing as reference someone developed a rest API which accepts multiple files as array which I'm trying to consume.

I see your api accepts map of files which if I use for my requirement will just overwrite all files and send only the last file. Could you some how add a new function which accepts slice of files to attach to form.

Please refer to SetFiles function in your code.

Please use the below function as enhancement

func (r *Request) SetFile(param, filePath []string) *Request {
	r.isMultiPart = true
	r.FormData.Set("@"+param, filePath)
	return r
}

As per url.Values, its a map of string and string slice

avinash92c avatar Jul 02 '20 06:07 avinash92c

@avinash92c I think you could achieve the behavior using SetFormDataFromValues. Can you try and share your feedback?

jeevatkm avatar Sep 04 '20 22:09 jeevatkm

@avinash92c Any updates?

jeevatkm avatar Oct 07 '20 06:10 jeevatkm

@jeevatkm I met same problem. The request just like this:

curl --location --request POST 'http://127.0.0.1:8080/' \
--form 'name="test"' \
--form 'file=@"123.jpg"' \
--form 'file=@"456.png"'

Like you said, using SetFormDataFromValues function can solve it. Maybe like this: @avinash92c

    var urlValues url.Values
    urlValues = make(map[string][]string)
    for _, f := range files {
        urlValues.Add("@"+f.Name, f.AbsolutePath)
    }

    client := resty.New()
    request := client.R()
    request.SetFormDataFromValues(urlValues)
    request.Post(url)
    .....

So maybe we can actually implement a method to meet this requirement.

    func (r *Request) SetFileArray(param, filePath []string) *resty.Request {
        r.isMultiPart = true
        for _, fp := range filePath {
            r.FormData.Add("@"+param, fp)
        }
        return r
    }

If you think need such a function, I can make a pr.

LinkinStars avatar Dec 11 '20 15:12 LinkinStars

I already solved this before making this post. i just thought a simpler and straight forward function might make things much easier to use for others facing same issue. @jeevatkm @LinkinStars Thanks for the responses. very cool alternative solutions.

avinash92c avatar Dec 11 '20 15:12 avinash92c

I met same problem.

ViCrack avatar Apr 03 '24 07:04 ViCrack

I already solved this before making this post. i just thought a simpler and straight forward function might make things much easier to use for others facing same issue. @jeevatkm @LinkinStars Thanks for the responses. very cool alternative solutions.

How did you solve it?

youngsailor avatar Apr 16 '24 07:04 youngsailor