Multiple files posting with same key
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 I think you could achieve the behavior using SetFormDataFromValues. Can you try and share your feedback?
@avinash92c Any updates?
@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.
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.
I met same problem.
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?