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: wklken/gorequest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: parnurzeal/gorequest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 6 commits
  • 4 files changed
  • 3 contributors

Commits on Apr 30, 2019

  1. add context support

    Pecado committed Apr 30, 2019
    Copy the full SHA
    6b756c0 View commit details

Commits on Nov 3, 2021

  1. Fix wrong example

    asjdf committed Nov 3, 2021
    Copy the full SHA
    5691731 View commit details

Commits on Feb 21, 2024

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e15d32a View commit details
  2. Merge pull request parnurzeal#266 from asjdf/develop

    Fix wrong example
    parnurzeal authored Feb 21, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    2f6d15e View commit details

Commits on Feb 22, 2024

  1. Copy the full SHA
    6e95a5f View commit details
  2. Merge pull request parnurzeal#279 from parnurzeal/develop

    Update Master to catch up with Dev branch
    parnurzeal authored Feb 22, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8900f38 View commit details
Showing with 71 additions and 1 deletion.
  1. +12 −0 go.mod
  2. +19 −0 go.sum
  3. +19 −1 gorequest.go
  4. +21 −0 gorequest_test.go
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module parnurzeal/gorequest

go 1.22.0

require (
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5
github.com/pkg/errors v0.9.1
golang.org/x/net v0.21.0
moul.io/http2curl v1.0.0
)

require github.com/smartystreets/goconvey v1.8.1 // indirect
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5 h1:m62nsMU279qRD9PQSWD1l66kmkXzuYcnVJqL4XLeV2M=
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
20 changes: 19 additions & 1 deletion gorequest.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package gorequest

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
@@ -86,6 +87,7 @@ type SuperAgent struct {
Retryable superAgentRetryable
DoNotClearSuperAgent bool
isClone bool
context context.Context
}

var DisableTransportSwap = false
@@ -118,6 +120,7 @@ func New() *SuperAgent {
CurlCommand: false,
logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags),
isClone: false,
context: nil,
}
// disable keep alives by default, see this issue https://github.com/parnurzeal/gorequest/issues/75
s.Transport.DisableKeepAlives = true
@@ -229,6 +232,7 @@ func (s *SuperAgent) Clone() *SuperAgent {
Retryable: copyRetryable(s.Retryable),
DoNotClearSuperAgent: true,
isClone: true,
context: s.context,
}
return clone
}
@@ -275,6 +279,7 @@ func (s *SuperAgent) ClearSuperAgent() {
s.TargetType = TypeJSON
s.Cookies = make([]*http.Cookie, 0)
s.Errors = nil
s.context = nil
}

// Just a wrapper to initialize SuperAgent instance by method string
@@ -392,7 +397,7 @@ func (s *SuperAgent) AppendHeader(param string, value string) *SuperAgent {

// gorequest.New().
// Post("/gamelist").
// Retry(3, 5 * time.seconds, http.StatusBadRequest, http.StatusInternalServerError).
// Retry(3, 5 * time.Second, http.StatusBadRequest, http.StatusInternalServerError).
// End()
func (s *SuperAgent) Retry(retryerCount int, retryerTime time.Duration, statusCode ...int) *SuperAgent {
for _, code := range statusCode {
@@ -1135,6 +1140,14 @@ func contains(respStatus int, statuses []int) bool {
return false
}

func (s *SuperAgent) Context(ctx context.Context) *SuperAgent{
if ctx == nil {
panic("context can't be nil")
}
s.context = ctx
return s
}

// EndStruct should be used when you want the body as a struct. The callbacks work the same way as with `End`, except that a struct is used instead of a string.
func (s *SuperAgent) EndStruct(v interface{}, callback ...func(response Response, v interface{}, body []byte, errs []error)) (Response, []byte, []error) {
resp, body, errs := s.EndBytes()
@@ -1376,6 +1389,11 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
if req, err = http.NewRequest(s.Method, s.Url, contentReader); err != nil {
return nil, err
}

if s.context != nil {
req = req.WithContext(s.context)
}

for k, vals := range s.Header {
for _, v := range vals {
req.Header.Add(k, v)
21 changes: 21 additions & 0 deletions gorequest_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package gorequest

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
@@ -2585,3 +2586,23 @@ func TestSetDebugByEnvironmentVar(t *testing.T) {
t.Fatalf("\nExpected gorequest not to log request and response object if GOREQUEST_DEBUG is not set.")
}
}

func TestContenxt(t *testing.T) {
// requesting a long connection which block this requst for a time,
// but 2 seconds later we unable to hold ourself back and force close it
ctx, cancel := context.WithCancel(context.Background())
go New().Context(ctx).Get("http://127.0.0.1:8080/foo").EndBytes(func(response Response, body []byte, errs []error) {


if len(errs) > 0 {
fmt.Printf("%+v\n", errs[0])
}
fmt.Println(string(body))
})

select {
case <-time.After(2 * time.Second):
fmt.Println("cancel...")
cancel()
}
}