Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multipart/form-data transport to support file uploads #268

Merged
merged 21 commits into from
Jul 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,43 @@ func prepareMultipartData(request *Request) (*prepareMultipartDataResult, error)
if err != nil {
return nil, err
}
fw.Write(input)
_, err = fw.Write(input)
if err != nil {
return nil, err
}
fw, err = mpw.CreateFormField("map")
if err != nil {
return nil, err
}
_, err = fw.Write([]byte("{"))
pkqk marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
fileMapEntries := []string{}
for fileIndex, path := range res.fileMap {
fw.Write([]byte(fmt.Sprintf(
"{\"%s\": [\"%s\"]}", fileIndex, path[0],
)))
fw, fileErr := mpw.CreateFormFile(fileIndex, res.files[fileIndex].Filename)
fileMapEntries = append(fileMapEntries,
fmt.Sprintf(
"\"%s\": [\"%s\"]", fileIndex, path[0],
),
)
}
_, err = fw.Write([]byte(strings.Join(fileMapEntries, ",")))
if err != nil {
return nil, err
}
_, err = fw.Write([]byte("}"))
if err != nil {
return nil, err
}
for fileIndex := range res.fileMap {
innerFw, fileErr := mpw.CreateFormFile(fileIndex, res.files[fileIndex].Filename)
if fileErr != nil {
return nil, fileErr
}
io.Copy(fw, res.files[fileIndex].File)
_, ioErr := io.Copy(innerFw, res.files[fileIndex].File)
if ioErr != nil {
return nil, ioErr
}
}
err = mpw.Close()
if err != nil {
Expand Down