Unverified Commit 3ed7a99f authored by Eric_Lee's avatar Eric_Lee Committed by GitHub

Merge pull request #132 from jumpserver/dev_bugfix

[Bugfix] fix unclosed fd issues
parents 4a509ca4 47105d55
......@@ -144,6 +144,9 @@ func (c *Client) NewRequest(method, url string, body interface{}, params []map[s
return
}
req, err = http.NewRequest(method, url, reader)
if err != nil {
return req, err
}
c.SetReqHeaders(req, params)
return req, err
}
......@@ -219,7 +222,7 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er
default:
attr, err := json.Marshal(val.Field(i).Interface())
if err != nil {
return nil
return err
}
v = string(attr)
}
......@@ -229,6 +232,9 @@ func (c *Client) PostForm(url string, data interface{}, res interface{}) (err er
reader := strings.NewReader(values.Encode())
req, err := http.NewRequest("POST", url, reader)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil
}
......@@ -238,6 +244,7 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params ..
if err != nil {
return err
}
defer f.Close()
buf := new(bytes.Buffer)
bodyWriter := multipart.NewWriter(buf)
gName := filepath.Base(gFile)
......@@ -245,18 +252,23 @@ func (c *Client) UploadFile(url string, gFile string, res interface{}, params ..
if err != nil {
return err
}
_, err = io.Copy(part, f)
if _, err = io.Copy(part, f); err != nil {
return err
}
err = bodyWriter.Close()
if err != nil {
return err
}
url = c.parseUrl(url, params)
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", bodyWriter.FormDataContentType())
c.SetReqHeaders(req, params)
resp, err := c.http.Do(req)
if err != nil {
return
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment