Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: go-resty/resty
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.13.1
Choose a base ref
...
head repository: go-resty/resty
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.14.0
Choose a head ref
  • 7 commits
  • 31 files changed
  • 4 contributors

Commits on Jun 28, 2024

  1. feat(curl): generate curl cmd for request && example for curl cmd (#794)

    * feat(curl): generate curl cmd for request && example for curl cmd
    
    * refactor(curl): Simplified code
    
    1. refactor `GetCurlCommand` with the name `GenerateCurlCommand`
    2. un-export this method `BuildCurlRequest`
    3. remove SetResultCurlCmd
    
    * cicd(test): add "-coverpkg=./..." to measure the test coverage of packages that are imported in different packages
    ahuigo authored Jun 28, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    855d418 View commit details
  2. resty dev version number and year update (#799)

    jeevatkm authored Jun 28, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7caa65b View commit details
  3. build: update bazel config with new files (#800)

    jeevatkm authored Jun 28, 2024

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    60a2149 View commit details

Commits on Jul 3, 2024

  1. fix(examples): wrongly stderr written as stdout (#801)

    ahuigo authored Jul 3, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    30477b3 View commit details

Commits on Jul 17, 2024

  1. fix: change resty.GET to resty.MethodGet in doc comment (#803)

    autopp authored Jul 17, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d8fbff6 View commit details

Commits on Jul 18, 2024

  1. update golang.org/x/net package for vuln CVE-2023-45288 (#804)

    shedyfreak authored Jul 18, 2024

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    87d5499 View commit details

Commits on Aug 5, 2024

  1. chore: for release v2.14.0 (#816)

    jeevatkm authored Aug 5, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c646328 View commit details
Showing with 509 additions and 41 deletions.
  1. +1 −1 .github/workflows/ci.yml
  2. +1 −1 .github/workflows/label-actions.yml
  3. +2 −1 .gitignore
  4. +2 −0 BUILD.bazel
  5. +1 −1 LICENSE
  6. +10 −2 README.md
  7. +17 −9 client.go
  8. +1 −1 client_test.go
  9. +1 −1 context_test.go
  10. +1 −1 digest.go
  11. +1 −1 example_test.go
  12. +126 −0 examples/debug_curl_test.go
  13. +162 −0 examples/server_test.go
  14. +2 −2 go.mod
  15. +23 −3 go.sum
  16. +13 −1 middleware.go
  17. +1 −1 redirect.go
  18. +22 −3 request.go
  19. +1 −1 request_test.go
  20. +1 −1 response.go
  21. +2 −2 resty.go
  22. +1 −1 resty_test.go
  23. +1 −1 retry.go
  24. +1 −1 retry_test.go
  25. +34 −0 shellescape/shellescape.go
  26. +1 −1 trace.go
  27. +1 −1 transport.go
  28. +1 −1 transport112.go
  29. +1 −1 util.go
  30. +76 −0 util_curl.go
  31. +1 −1 util_test.go
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ jobs:
run: diff -u <(echo -n) <(go fmt $(go list ./...))

- name: Test
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
2 changes: 1 addition & 1 deletion .github/workflows/label-actions.yml
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ jobs:
cache-dependency-path: go.sum

- name: Test
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./...

- name: Coverage
run: bash <(curl -s https://codecov.io/bash)
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -26,5 +26,6 @@ _testmain.go
coverage.out
coverage.txt

# Exclude intellij IDE folders
# Exclude IDE folders
.idea/*
.vscode/*
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ go_library(
"transport.go",
"transport112.go",
"util.go",
"util_curl.go",
"shellescape/shellescape.go"
],
importpath = "github.com/go-resty/resty/v2",
visibility = ["//visibility:public"],
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2023 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>
Copyright (c) 2015-2024 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<p align="center"><a href="#features">Features</a> section describes in detail about Resty capabilities</p>
</p>
<p align="center">
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Amaster"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg" alt="Build Status"></a> <a href="https://codecov.io/gh/go-resty/resty/branch/master"><img src="https://codecov.io/gh/go-resty/resty/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.13.1-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Amaster"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg" alt="Build Status"></a> <a href="https://codecov.io/gh/go-resty/resty/branch/master"><img src="https://codecov.io/gh/go-resty/resty/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.14.0-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
</p>
<p align="center">
<h4 align="center">Resty Communication Channels</h4>
@@ -13,7 +13,7 @@

## News

* v2.13.1 [released](https://github.com/go-resty/resty/releases/tag/v2.13.1) and tagged on May 10, 2024.
* v2.14.0 [released](https://github.com/go-resty/resty/releases/tag/v2.14.0) and tagged on Aug 04, 2024.
* v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019.
* v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019.
* v1.0 released and tagged on Sep 25, 2017. - Resty's first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I'm very thankful to Resty users and its [contributors](https://github.com/go-resty/resty/graphs/contributors).
@@ -131,6 +131,11 @@ client := resty.New()
resp, err := client.R().
EnableTrace().
Get("https://httpbin.org/get")
curlCmdExecuted := resp.Request.GenerateCurlCommand()


// Explore curl command
fmt.Println("Curl Command:\n ", curlCmdExecuted+"\n")

// Explore response object
fmt.Println("Response Info:")
@@ -160,6 +165,9 @@ fmt.Println(" RequestAttempt:", ti.RequestAttempt)
fmt.Println(" RemoteAddr :", ti.RemoteAddr.String())

/* Output
Curl Command:
curl -X GET -H 'User-Agent: go-resty/2.12.0 (https://github.com/go-resty/resty)' https://httpbin.org/get
Response Info:
Error : <nil>
Status Code: 200
26 changes: 17 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// Copyright (c) 2015-2024 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

@@ -1148,9 +1148,7 @@ func (c *Client) Clone() *Client {
// Client Unexported methods
//_______________________________________________________________________

// Executes method executes the given `Request` object and returns response
// error.
func (c *Client) execute(req *Request) (*Response, error) {
func (c *Client) executeBefore(req *Request) error {
// Lock the user-defined pre-request hooks.
c.udBeforeRequestLock.RLock()
defer c.udBeforeRequestLock.RUnlock()
@@ -1166,22 +1164,22 @@ func (c *Client) execute(req *Request) (*Response, error) {
// to modify the *resty.Request object
for _, f := range c.udBeforeRequest {
if err = f(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

// If there is a rate limiter set for this client, the Execute call
// will return an error if the rate limit is exceeded.
if req.client.rateLimiter != nil {
if !req.client.rateLimiter.Allow() {
return nil, wrapNoRetryErr(ErrRateLimitExceeded)
return wrapNoRetryErr(ErrRateLimitExceeded)
}
}

// resty middlewares
for _, f := range c.beforeRequest {
if err = f(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

@@ -1192,15 +1190,24 @@ func (c *Client) execute(req *Request) (*Response, error) {
// call pre-request if defined
if c.preReqHook != nil {
if err = c.preReqHook(c, req.RawRequest); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

if err = requestLogger(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}

req.RawRequest.Body = newRequestBodyReleaser(req.RawRequest.Body, req.bodyBuf)
return nil
}

// Executes method executes the given `Request` object and returns response
// error.
func (c *Client) execute(req *Request) (*Response, error) {
if err := c.executeBefore(req); err != nil {
return nil, err
}

req.Time = time.Now()
resp, err := c.httpClient.Do(req.RawRequest)
@@ -1396,6 +1403,7 @@ func createClient(hc *http.Client) *Client {
parseRequestBody,
createHTTPRequest,
addCredentials,
createCurlCmd,
}

// user defined request middlewares
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// Copyright (c) 2015-2024 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com)
// Copyright (c) 2015-2024 Jeevanandam M (jeeva@myjeeva.com)
// 2016 Andrew Grigorev (https://github.com/ei-grad)
// All rights reserved.
// resty source code and usage is governed by a MIT style
2 changes: 1 addition & 1 deletion digest.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com)
// Copyright (c) 2015-2024 Jeevanandam M (jeeva@myjeeva.com)
// 2023 Segev Dagan (https://github.com/segevda)
// 2024 Philipp Wolfer (https://github.com/phw)
// All rights reserved.
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 Jeevanandam M. (jeeva@myjeeva.com), All rights reserved.
// Copyright (c) 2015-2024 Jeevanandam M. (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

126 changes: 126 additions & 0 deletions examples/debug_curl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package examples

import (
"io"
"net/http"
"os"
"strings"
"testing"

"github.com/go-resty/resty/v2"
)

// 1. Generate curl for unexecuted request(dry-run)
func TestGenerateUnexcutedCurl(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()

req := resty.New().R().SetBody(map[string]string{
"name": "Alex",
}).SetCookies(
[]*http.Cookie{
{Name: "count", Value: "1"},
},
)

curlCmdUnexecuted := req.GenerateCurlCommand()

if !strings.Contains(curlCmdUnexecuted, "Cookie: count=1") ||
!strings.Contains(curlCmdUnexecuted, "curl -X GET") ||
!strings.Contains(curlCmdUnexecuted, `-d '{"name":"Alex"}'`) {
t.Fatal("Incomplete curl:", curlCmdUnexecuted)
} else {
t.Log("curlCmdUnexecuted: \n", curlCmdUnexecuted)
}

}

// 2. Generate curl for executed request
func TestGenerateExecutedCurl(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()

data := map[string]string{
"name": "Alex",
}
req := resty.New().R().SetBody(data).SetCookies(
[]*http.Cookie{
{Name: "count", Value: "1"},
},
)

url := ts.URL + "/post"
resp, err := req.
EnableTrace().
Post(url)
if err != nil {
t.Fatal(err)
}
curlCmdExecuted := resp.Request.GenerateCurlCommand()
if !strings.Contains(curlCmdExecuted, "Cookie: count=1") ||
!strings.Contains(curlCmdExecuted, "curl -X POST") ||
!strings.Contains(curlCmdExecuted, `-d '{"name":"Alex"}'`) ||
!strings.Contains(curlCmdExecuted, url) {
t.Fatal("Incomplete curl:", curlCmdExecuted)
} else {
t.Log("curlCmdExecuted: \n", curlCmdExecuted)
}
}

// 3. Generate curl in debug mode
func TestDebugModeCurl(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()

// 1. Capture stderr
getOutput, restore := captureStderr()
defer restore()

// 2. Build request
req := resty.New().R().SetBody(map[string]string{
"name": "Alex",
}).SetCookies(
[]*http.Cookie{
{Name: "count", Value: "1"},
},
)

// 3. Execute request: set debug mode
url := ts.URL + "/post"
_, err := req.SetDebug(true).Post(url)
if err != nil {
t.Fatal(err)
}

// 4. test output curl
output := getOutput()
if !strings.Contains(output, "Cookie: count=1") ||
!strings.Contains(output, `-d '{"name":"Alex"}'`) {
t.Fatal("Incomplete debug curl info:", output)
} else {
t.Log("Normal debug curl info: \n", output)
}
}

func captureStderr() (getOutput func() string, restore func()) {
old := os.Stderr
r, w, err := os.Pipe()
if err != nil {
panic(err)
}
os.Stderr = w
getOutput = func() string {
w.Close()
buf := make([]byte, 2048)
n, err := r.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
return string(buf[:n])
}
restore = func() {
os.Stderr = old
w.Close()
}
return getOutput, restore
}
Loading