diff --git a/client.go b/client.go
index ec0e54a2..93893b71 100644
--- a/client.go
+++ b/client.go
@@ -29,26 +29,26 @@ import (
)
const (
- // GET HTTP method
- GET = "GET"
+ // MethodGet HTTP method
+ MethodGet = "GET"
- // POST HTTP method
- POST = "POST"
+ // MethodPost HTTP method
+ MethodPost = "POST"
- // PUT HTTP method
- PUT = "PUT"
+ // MethodPut HTTP method
+ MethodPut = "PUT"
- // DELETE HTTP method
- DELETE = "DELETE"
+ // MethodDelete HTTP method
+ MethodDelete = "DELETE"
- // PATCH HTTP method
- PATCH = "PATCH"
+ // MethodPatch HTTP method
+ MethodPatch = "PATCH"
- // HEAD HTTP method
- HEAD = "HEAD"
+ // MethodHead HTTP method
+ MethodHead = "HEAD"
- // OPTIONS HTTP method
- OPTIONS = "OPTIONS"
+ // MethodOptions HTTP method
+ MethodOptions = "OPTIONS"
)
var (
@@ -505,8 +505,8 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.Log.Printf("ERROR [%v]", err)
return nil, err
}
- conn.SetDeadline(time.Now().Add(timeout))
- return conn, nil
+ err = conn.SetDeadline(time.Now().Add(timeout))
+ return conn, err
}
return c
@@ -657,7 +657,9 @@ func (c *Client) execute(req *Request) (*Response, error) {
}
if !req.isSaveResponse {
- defer resp.Body.Close()
+ defer func() {
+ _ = resp.Body.Close()
+ }()
response.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
@@ -870,7 +872,9 @@ func addFile(w *multipart.Writer, fieldName, path string) error {
if err != nil {
return err
}
- defer file.Close()
+ defer func() {
+ _ = file.Close()
+ }()
part, err := w.CreateFormFile(fieldName, filepath.Base(path))
if err != nil {
@@ -900,7 +904,7 @@ func getPointer(v interface{}) interface{} {
}
func isPayloadSupported(m string) bool {
- return (m == POST || m == PUT || m == DELETE || m == PATCH)
+ return (m == MethodPost || m == MethodPut || m == MethodDelete || m == MethodPatch)
}
func typeOf(i interface{}) reflect.Type {
diff --git a/example_test.go b/example_test.go
index 0b360f84..e6a623e9 100644
--- a/example_test.go
+++ b/example_test.go
@@ -161,11 +161,13 @@ func Example_customRootCertificate() {
func ExampleNew() {
// Creating client1
client1 := resty.New()
- client1.R().Get("http://httpbin.org/get")
+ resp1, err1 := client1.R().Get("http://httpbin.org/get")
+ fmt.Println(resp1, err1)
// Creating client2
client2 := resty.New()
- client2.R().Get("http://httpbin.org/get")
+ resp2, err2 := client2.R().Get("http://httpbin.org/get")
+ fmt.Println(resp2, err2)
}
//
diff --git a/middleware.go b/middleware.go
index 7ebe8450..4ccbffc9 100644
--- a/middleware.go
+++ b/middleware.go
@@ -96,7 +96,7 @@ func parseRequestBody(c *Client, r *Request) (err error) {
if isPayloadSupported(r.Method) {
// Handling Multipart
- if r.isMultiPart && !(r.Method == PATCH) {
+ if r.isMultiPart && !(r.Method == MethodPatch) {
if err = handleMultipart(c, r); err != nil {
return
}
@@ -271,7 +271,9 @@ func handleMultipart(c *Client, r *Request) (err error) {
for k, v := range c.FormData {
for _, iv := range v {
- w.WriteField(k, iv)
+ if err = w.WriteField(k, iv); err != nil {
+ return err
+ }
}
}
@@ -283,7 +285,9 @@ func handleMultipart(c *Client, r *Request) (err error) {
return
}
} else { // form value
- w.WriteField(k, iv)
+ if err = w.WriteField(k, iv); err != nil {
+ return err
+ }
}
}
}
@@ -343,7 +347,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
if reader, ok := r.Body.(io.Reader); ok {
r.bodyBuf = &bytes.Buffer{}
- r.bodyBuf.ReadFrom(reader)
+ _, err = r.bodyBuf.ReadFrom(reader)
} else if b, ok := r.Body.([]byte); ok {
bodyBytes = b
} else if s, ok := r.Body.(string); ok {
@@ -390,10 +394,14 @@ func saveResponseIntoFile(c *Client, res *Response) error {
if err != nil {
return err
}
- defer outFile.Close()
+ defer func() {
+ _ = outFile.Close()
+ }()
// io.Copy reads maximum 32kb size, it is perfect for large file download too
- defer res.RawResponse.Body.Close()
+ defer func() {
+ _ = res.RawResponse.Body.Close()
+ }()
written, err := io.Copy(outFile, res.RawResponse.Body)
if err != nil {
return err
diff --git a/request.go b/request.go
index aba75180..60cbbfb3 100644
--- a/request.go
+++ b/request.go
@@ -383,37 +383,37 @@ func (r *Request) SetProxy(proxyURL string) *Request {
// Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.
func (r *Request) Get(url string) (*Response, error) {
- return r.Execute(GET, url)
+ return r.Execute(MethodGet, url)
}
// Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231.
func (r *Request) Head(url string) (*Response, error) {
- return r.Execute(HEAD, url)
+ return r.Execute(MethodHead, url)
}
// Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
func (r *Request) Post(url string) (*Response, error) {
- return r.Execute(POST, url)
+ return r.Execute(MethodPost, url)
}
// Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231.
func (r *Request) Put(url string) (*Response, error) {
- return r.Execute(PUT, url)
+ return r.Execute(MethodPut, url)
}
// Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231.
func (r *Request) Delete(url string) (*Response, error) {
- return r.Execute(DELETE, url)
+ return r.Execute(MethodDelete, url)
}
// Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231.
func (r *Request) Options(url string) (*Response, error) {
- return r.Execute(OPTIONS, url)
+ return r.Execute(MethodOptions, url)
}
// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789.
func (r *Request) Patch(url string) (*Response, error) {
- return r.Execute(PATCH, url)
+ return r.Execute(MethodPatch, url)
}
// Execute method performs the HTTP request with given HTTP method and URL
@@ -421,7 +421,7 @@ func (r *Request) Patch(url string) (*Response, error) {
// resp, err := resty.R().Execute(resty.GET, "http://httpbin.org/get")
//
func (r *Request) Execute(method, url string) (*Response, error) {
- if r.isMultiPart && !(method == POST || method == PUT) {
+ if r.isMultiPart && !(method == MethodPost || method == MethodPut) {
return nil, fmt.Errorf("Multipart content is not allowed in HTTP verb [%v]", method)
}
diff --git a/resty.go b/resty.go
index b5255a7e..a498e44a 100644
--- a/resty.go
+++ b/resty.go
@@ -6,4 +6,4 @@
package resty
// Version # of go-resty
-var Version = "0.9"
+var Version = "0.10"
diff --git a/resty_test.go b/resty_test.go
index dd437a1e..7eb82212 100644
--- a/resty_test.go
+++ b/resty_test.go
@@ -904,7 +904,7 @@ func TestClientTimeoutInternalError(t *testing.T) {
c.SetHTTPMode()
c.SetTimeout(time.Duration(time.Second * 1))
- c.R().Get("http://localhost:9000/set-timeout-test")
+ _, _ = c.R().Get("http://localhost:9000/set-timeout-test")
}
func TestHeadMethod(t *testing.T) {
@@ -1148,7 +1148,7 @@ func TestMuliParamQueryString(t *testing.T) {
client.SetQueryParam("status", "open")
- req1.SetQueryParam("status", "pending").
+ _, _ = req1.SetQueryParam("status", "pending").
SetQueryParam("status", "approved").
Get(ts1.URL)
@@ -1167,7 +1167,7 @@ func TestMuliParamQueryString(t *testing.T) {
"status": []string{"pending", "approved", "reject"},
}
- req2.SetMultiValueQueryParams(v).Get(ts2.URL)
+ _, _ = req2.SetMultiValueQueryParams(v).Get(ts2.URL)
assertEqual(t, true, strings.Contains(req2.URL, "status=pending"))
assertEqual(t, true, strings.Contains(req2.URL, "status=approved"))
@@ -1417,28 +1417,28 @@ func createGetServer(t *testing.T) *httptest.Server {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
- if r.Method == GET {
+ if r.Method == MethodGet {
if r.URL.Path == "/" {
- w.Write([]byte("TestGet: text response"))
+ _, _ = w.Write([]byte("TestGet: text response"))
} else if r.URL.Path == "/mypage" {
w.WriteHeader(http.StatusBadRequest)
} else if r.URL.Path == "/mypage2" {
- w.Write([]byte("TestGet: text response from mypage2"))
+ _, _ = w.Write([]byte("TestGet: text response from mypage2"))
} else if r.URL.Path == "/set-retrycount-test" {
attempt++
if attempt != 3 {
time.Sleep(time.Second * 6)
}
- w.Write([]byte("TestClientRetry page"))
+ _, _ = w.Write([]byte("TestClientRetry page"))
} else if r.URL.Path == "/set-timeout-test" {
time.Sleep(time.Second * 6)
- w.Write([]byte("TestClientTimeout page"))
+ _, _ = w.Write([]byte("TestClientTimeout page"))
} else if r.URL.Path == "/my-image.png" {
fileBytes, _ := ioutil.ReadFile(getTestDataPath() + "/test-img.png")
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(fileBytes)))
- w.Write(fileBytes)
+ _, _ = w.Write(fileBytes)
}
}
})
@@ -1458,18 +1458,18 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
if err != nil {
t.Logf("Error: %#v", err)
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
+ _, _ = w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
return
}
if user.Username == "testuser" && user.Password == "testpass" {
- w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
+ _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
} else if user.Username == "testuser" && user.Password == "invalidjson" {
- w.Write([]byte(`{ "id": "success", "message": "login successful", }`))
+ _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful", }`))
} else {
w.Header().Set("Www-Authenticate", "Protected Realm")
w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
+ _, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
}
return
@@ -1484,22 +1484,22 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
if err != nil {
t.Logf("Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(``))
- w.Write([]byte(`bad_requestUnable to read user info`))
+ _, _ = w.Write([]byte(``))
+ _, _ = w.Write([]byte(`bad_requestUnable to read user info`))
return
}
if user.Username == "testuser" && user.Password == "testpass" {
- w.Write([]byte(``))
- w.Write([]byte(`successlogin successful`))
+ _, _ = w.Write([]byte(``))
+ _, _ = w.Write([]byte(`successlogin successful`))
} else if user.Username == "testuser" && user.Password == "invalidxml" {
- w.Write([]byte(``))
- w.Write([]byte(`successlogin successful`))
+ _, _ = w.Write([]byte(``))
+ _, _ = w.Write([]byte(`successlogin successful`))
} else {
w.Header().Set("Www-Authenticate", "Protected Realm")
w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(``))
- w.Write([]byte(`unauthorizedInvalid credentials`))
+ _, _ = w.Write([]byte(``))
+ _, _ = w.Write([]byte(`unauthorizedInvalid credentials`))
}
return
@@ -1518,7 +1518,7 @@ func handleUsersEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
if err != nil {
t.Logf("Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
+ _, _ = w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
return
}
@@ -1526,14 +1526,14 @@ func handleUsersEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
if len(users) != 3 {
t.Log("Error: Excepted count of 3 records")
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(`{ "id": "bad_request", "message": "Expected record count doesn't match" }`))
+ _, _ = w.Write([]byte(`{ "id": "bad_request", "message": "Expected record count doesn't match" }`))
return
}
eu := users[2]
if eu.FirstName == "firstname3" && eu.ZipCode == "10003" {
w.WriteHeader(http.StatusAccepted)
- w.Write([]byte(`{ "message": "Accepted" }`))
+ _, _ = w.Write([]byte(`{ "message": "Accepted" }`))
}
return
@@ -1547,7 +1547,7 @@ func createPostServer(t *testing.T) *httptest.Server {
t.Logf("Path: %v", r.URL.Path)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))
- if r.Method == POST {
+ if r.Method == MethodPost {
handleLoginEndpoint(t, w, r)
handleUsersEndpoint(t, w, r)
@@ -1562,7 +1562,7 @@ func createPostServer(t *testing.T) *httptest.Server {
if err != nil {
t.Logf("Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
+ _, _ = w.Write([]byte(`{ "id": "bad_request", "message": "Unable to read user info" }`))
return
}
@@ -1570,12 +1570,12 @@ func createPostServer(t *testing.T) *httptest.Server {
if len(users) != 1 {
t.Log("Error: Excepted count of 1 map records")
w.WriteHeader(http.StatusBadRequest)
- w.Write([]byte(`{ "id": "bad_request", "message": "Expected record count doesn't match" }`))
+ _, _ = w.Write([]byte(`{ "id": "bad_request", "message": "Expected record count doesn't match" }`))
return
}
w.WriteHeader(http.StatusAccepted)
- w.Write([]byte(`{ "message": "Accepted" }`))
+ _, _ = w.Write([]byte(`{ "message": "Accepted" }`))
return
}
@@ -1592,8 +1592,8 @@ func createFormPostServer(t *testing.T) *httptest.Server {
t.Logf("Path: %v", r.URL.Path)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))
- if r.Method == POST {
- r.ParseMultipartForm(10e6)
+ if r.Method == MethodPost {
+ _ = r.ParseMultipartForm(10e6)
if r.URL.Path == "/profile" {
t.Logf("FirstName: %v", r.FormValue("first_name"))
@@ -1601,7 +1601,7 @@ func createFormPostServer(t *testing.T) *httptest.Server {
t.Logf("City: %v", r.FormValue("city"))
t.Logf("Zip Code: %v", r.FormValue("zip_code"))
- w.Write([]byte("Success"))
+ _, _ = w.Write([]byte("Success"))
return
} else if r.URL.Path == "/search" {
formEncodedData := r.Form.Encode()
@@ -1610,14 +1610,14 @@ func createFormPostServer(t *testing.T) *httptest.Server {
assertEqual(t, true, strings.Contains(formEncodedData, "search_criteria=pencil"))
assertEqual(t, true, strings.Contains(formEncodedData, "search_criteria=glass"))
- w.Write([]byte("Success"))
+ _, _ = w.Write([]byte("Success"))
return
} else if r.URL.Path == "/upload" {
t.Logf("FirstName: %v", r.FormValue("first_name"))
t.Logf("LastName: %v", r.FormValue("last_name"))
targetPath := getTestDataPath() + "/upload"
- os.MkdirAll(targetPath, 0700)
+ _ = os.MkdirAll(targetPath, 0700)
for _, fhdrs := range r.MultipartForm.File {
for _, hdr := range fhdrs {
@@ -1634,10 +1634,12 @@ func createFormPostServer(t *testing.T) *httptest.Server {
t.Logf("Error: %v", err)
return
}
- defer f.Close()
- io.Copy(f, infile)
+ defer func() {
+ _ = f.Close()
+ }()
+ _, _ = io.Copy(f, infile)
- w.Write([]byte(fmt.Sprintf("File: %v, uploaded as: %v\n", hdr.Filename, fname)))
+ _, _ = w.Write([]byte(fmt.Sprintf("File: %v, uploaded as: %v\n", hdr.Filename, fname)))
}
}
@@ -1655,7 +1657,7 @@ func createAuthServer(t *testing.T) *httptest.Server {
t.Logf("Path: %v", r.URL.Path)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))
- if r.Method == GET {
+ if r.Method == MethodGet {
if r.URL.Path == "/profile" {
// 004DDB79-6801-4587-B976-F093E6AC44FF
auth := r.Header.Get("Authorization")
@@ -1666,20 +1668,20 @@ func createAuthServer(t *testing.T) *httptest.Server {
if !strings.HasPrefix(auth, "Bearer ") {
w.Header().Set("Www-Authenticate", "Protected Realm")
w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
+ _, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
return
}
if auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF" || auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF-Request" {
- w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
+ _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
}
}
return
}
- if r.Method == POST {
+ if r.Method == MethodPost {
if r.URL.Path == "/login" {
auth := r.Header.Get("Authorization")
t.Logf("Basic Auth: %v", auth)
@@ -1690,12 +1692,12 @@ func createAuthServer(t *testing.T) *httptest.Server {
if err != nil || string(password) != "myuser:basicauth" {
w.Header().Set("Www-Authenticate", "Protected Realm")
w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
+ _, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
return
}
- w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
+ _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
}
return
@@ -1710,26 +1712,26 @@ func createGenServer(t *testing.T) *httptest.Server {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
- if r.Method == PUT {
+ if r.Method == MethodPut {
if r.URL.Path == "/plaintext" {
- w.Write([]byte("TestPut: plain text response"))
+ _, _ = w.Write([]byte("TestPut: plain text response"))
} else if r.URL.Path == "/json" {
w.Header().Set(hdrContentTypeKey, jsonContentType)
- w.Write([]byte(`{"response":"json response"}`))
+ _, _ = w.Write([]byte(`{"response":"json response"}`))
} else if r.URL.Path == "/xml" {
w.Header().Set(hdrContentTypeKey, "application/xml")
- w.Write([]byte(`XML response`))
+ _, _ = w.Write([]byte(`XML response`))
}
}
- if r.Method == OPTIONS && r.URL.Path == "/options" {
+ if r.Method == MethodOptions && r.URL.Path == "/options" {
w.Header().Set("Access-Control-Allow-Origin", "localhost")
w.Header().Set("Access-Control-Allow-Methods", "PUT, PATCH")
w.Header().Set("Access-Control-Expose-Headers", "x-go-resty-id")
w.WriteHeader(http.StatusOK)
}
- if r.Method == PATCH && r.URL.Path == "/patch" {
+ if r.Method == MethodPatch && r.URL.Path == "/patch" {
w.WriteHeader(http.StatusOK)
}
})
@@ -1742,7 +1744,7 @@ func createRedirectServer(t *testing.T) *httptest.Server {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
- if r.Method == GET {
+ if r.Method == MethodGet {
if strings.HasPrefix(r.URL.Path, "/redirect-host-check-") {
cntStr := strings.SplitAfter(r.URL.Path, "-")[3]
cnt, _ := strconv.Atoi(cntStr)