lib/rest: allow Form upload with no file to upload

This commit is contained in:
Nick Craig-Wood 2019-07-06 14:36:17 +01:00
parent 8e8b78d7e5
commit 71587344c6

View File

@ -286,12 +286,12 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
// MultipartUpload creates an io.Reader which produces an encoded a // MultipartUpload creates an io.Reader which produces an encoded a
// multipart form upload from the params passed in and the passed in // multipart form upload from the params passed in and the passed in
// //
// in - the body of the file // in - the body of the file (may be nil)
// params - the form parameters // params - the form parameters
// fileName - is the name of the attached file // fileName - is the name of the attached file
// contentName - the name of the parameter for the file // contentName - the name of the parameter for the file
// //
// the *int64 returned is the overhead in addition to the file contents, in case Content-Length is required // the int64 returned is the overhead in addition to the file contents, in case Content-Length is required
// //
// NB This doesn't allow setting the content type of the attachment // NB This doesn't allow setting the content type of the attachment
func MultipartUpload(in io.Reader, params url.Values, contentName, fileName string) (io.ReadCloser, string, int64, error) { func MultipartUpload(in io.Reader, params url.Values, contentName, fileName string) (io.ReadCloser, string, int64, error) {
@ -315,10 +315,12 @@ func MultipartUpload(in io.Reader, params url.Values, contentName, fileName stri
} }
} }
} }
if in != nil {
_, err = dummyMultipartWriter.CreateFormFile(contentName, fileName) _, err = dummyMultipartWriter.CreateFormFile(contentName, fileName)
if err != nil { if err != nil {
return nil, "", 0, err return nil, "", 0, err
} }
}
err = dummyMultipartWriter.Close() err = dummyMultipartWriter.Close()
if err != nil { if err != nil {
@ -341,6 +343,7 @@ func MultipartUpload(in io.Reader, params url.Values, contentName, fileName stri
} }
} }
if in != nil {
part, err := writer.CreateFormFile(contentName, fileName) part, err := writer.CreateFormFile(contentName, fileName)
if err != nil { if err != nil {
_ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to create form file")) _ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to create form file"))
@ -352,6 +355,7 @@ func MultipartUpload(in io.Reader, params url.Values, contentName, fileName stri
_ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to copy data")) _ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to copy data"))
return return
} }
}
err = writer.Close() err = writer.Close()
if err != nil { if err != nil {
@ -423,8 +427,7 @@ func (api *Client) callCodec(opts *Opts, request interface{}, response interface
opts.Body = bytes.NewBuffer(requestBody) opts.Body = bytes.NewBuffer(requestBody)
} }
} }
isMultipart := (opts.MultipartParams != nil || opts.MultipartContentName != "") && opts.Body != nil if opts.MultipartParams != nil || opts.MultipartContentName != "" {
if isMultipart {
params := opts.MultipartParams params := opts.MultipartParams
if params == nil { if params == nil {
params = url.Values{} params = url.Values{}