Skip to content

Commit

Permalink
Merge pull request #908 from aubelsb2/tokenismissing
Browse files Browse the repository at this point in the history
Seems the token was missing from the request.
  • Loading branch information
kanata2 authored Apr 17, 2021
2 parents 93fe17c + 00a3998 commit e0d14cc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 3 additions & 5 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ func (api *Client) UploadFileContext(ctx context.Context, params FileUploadParam
return nil, err
}
response := &fileResponseFull{}
values := url.Values{
"token": {api.token},
}
values := url.Values{}
if params.Filetype != "" {
values.Add("filetype", params.Filetype)
}
Expand All @@ -320,12 +318,12 @@ func (api *Client) UploadFileContext(ctx context.Context, params FileUploadParam
values.Add("content", params.Content)
err = api.postMethod(ctx, "files.upload", values, response)
} else if params.File != "" {
err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.File, "file", values, response, api)
err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.File, "file", api.token, values, response, api)
} else if params.Reader != nil {
if params.Filename == "" {
return nil, fmt.Errorf("files.upload: FileUploadParameters.Filename is mandatory when using FileUploadParameters.Reader")
}
err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.Filename, "file", values, params.Reader, response, api)
err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.Filename, "file", api.token, values, params.Reader, response, api)
}

if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func parseResponseBody(body io.ReadCloser, intf interface{}, d Debug) error {
return json.Unmarshal(response, intf)
}

func postLocalWithMultipartResponse(ctx context.Context, client httpClient, method, fpath, fieldname string, values url.Values, intf interface{}, d Debug) error {
func postLocalWithMultipartResponse(ctx context.Context, client httpClient, method, fpath, fieldname, token string, values url.Values, intf interface{}, d Debug) error {
fullpath, err := filepath.Abs(fpath)
if err != nil {
return err
Expand All @@ -146,10 +146,10 @@ func postLocalWithMultipartResponse(ctx context.Context, client httpClient, meth
}
defer file.Close()

return postWithMultipartResponse(ctx, client, method, filepath.Base(fpath), fieldname, values, file, intf, d)
return postWithMultipartResponse(ctx, client, method, filepath.Base(fpath), fieldname, token, values, file, intf, d)
}

func postWithMultipartResponse(ctx context.Context, client httpClient, path, name, fieldname string, values url.Values, r io.Reader, intf interface{}, d Debug) error {
func postWithMultipartResponse(ctx context.Context, client httpClient, path, name, fieldname, token string, values url.Values, r io.Reader, intf interface{}, d Debug) error {
pipeReader, pipeWriter := io.Pipe()
wr := multipart.NewWriter(pipeWriter)
errc := make(chan error)
Expand All @@ -175,6 +175,7 @@ func postWithMultipartResponse(ctx context.Context, client httpClient, path, nam
return err
}
req.Header.Add("Content-Type", wr.FormDataContentType())
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req = req.WithContext(ctx)
resp, err := client.Do(req)

Expand Down
2 changes: 1 addition & 1 deletion users.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (api *Client) SetUserPhotoContext(ctx context.Context, image string, params
values.Add("crop_w", strconv.Itoa(params.CropW))
}

err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"users.setPhoto", image, "image", values, response, api)
err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"users.setPhoto", image, "image", api.token, values, response, api)
if err != nil {
return err
}
Expand Down

0 comments on commit e0d14cc

Please sign in to comment.