mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
+6
-4
@@ -109,10 +109,13 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
|
||||
|
||||
var r io.Reader
|
||||
if body != nil {
|
||||
if data, ok := body.([]byte); ok {
|
||||
switch data := body.(type) {
|
||||
case io.Reader:
|
||||
r = data
|
||||
case []byte:
|
||||
r = bytes.NewReader(data)
|
||||
} else {
|
||||
// Assume JSON if not bytes.
|
||||
default:
|
||||
// Assume JSON in all other cases.
|
||||
buf := bytes.NewBuffer(nil)
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
@@ -120,7 +123,6 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("encode body: %w", err)
|
||||
}
|
||||
|
||||
r = buf
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -21,8 +21,8 @@ type UploadResponse struct {
|
||||
|
||||
// Upload uploads an arbitrary file with the content type provided.
|
||||
// This is used to upload a source-code archive.
|
||||
func (c *Client) Upload(ctx context.Context, contentType string, content []byte) (UploadResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodPost, "/api/v2/files", content, func(r *http.Request) {
|
||||
func (c *Client) Upload(ctx context.Context, contentType string, rd io.Reader) (UploadResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodPost, "/api/v2/files", rd, func(r *http.Request) {
|
||||
r.Header.Set("Content-Type", contentType)
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user