Skip to content

Commit

Permalink
fix form data encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
zmxv committed Oct 15, 2018
1 parent e8e2be8 commit a9a170d
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,44 +226,14 @@ func (c *APIClient) prepareRequest(
}
}

// add form parameters and file if available.
if len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
// Add form parameters
if len(formParams) > 0 {
if body != nil {
return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
}
body = &bytes.Buffer{}
w := multipart.NewWriter(body)

for k, v := range formParams {
for _, iv := range v {
if strings.HasPrefix(k, "@") { // file
err = addFile(w, k[1:], iv)
if err != nil {
return nil, err
}
} else { // form value
w.WriteField(k, iv)
}
}
}
if len(fileBytes) > 0 && fileName != "" {
w.Boundary()
//_, fileNm := filepath.Split(fileName)
part, err := w.CreateFormFile("file", filepath.Base(fileName))
if err != nil {
return nil, err
}
_, err = part.Write(fileBytes)
if err != nil {
return nil, err
}
// Set the Boundary in the Content-Type
headerParams["Content-Type"] = w.FormDataContentType()
}

// Set Content-Length
body = bytes.NewBuffer([]byte(formParams.Encode()))
headerParams["Content-Type"] = "application/x-www-form-urlencoded"
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
w.Close()
}

// Setup path and query parameters
Expand Down

0 comments on commit a9a170d

Please sign in to comment.