go-resty / resty

Simple HTTP and REST client library for Go
MIT License
10.12k stars 710 forks source link

Unable to send custom header #626

Closed itziklavon closed 1 month ago

itziklavon commented 1 year ago

Hi, Hi, i'm getting some error when testing some code which its response with status 30X it expects location header, but when i run tests locally, i need to ignore it, how can we do it?

currently i've defined some round tripper but it is pretty ugly workaround, is there a simpler way? i've tested some other web clients, on some i did not get this error, this is what i currently have defined:


    // e.g. patch the request before send it
    var roundTripper = &http.Transport{
        DialContext: (&net.Dialer{
            Timeout:   10 * time.Second,
            KeepAlive: 30 * time.Second,
        }).DialContext,
        ForceAttemptHTTP2:     false,
        MaxIdleConns:          utils.GetOrDefaultInt(viper.GetString("HTTP_CONNECTION_POOL_MAX_SIZE"), 5000),
        MaxConnsPerHost:       utils.GetOrDefaultInt(viper.GetString("HTTP_CONNECTION_POOL_MAX_SIZE_PER_HOST"), 20),
        MaxIdleConnsPerHost:   utils.GetOrDefaultInt(viper.GetString("HTTP_CONNECTION_POOL_MAX_IDLE_SIZE_PER_HOST"), 10),
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }
    resp, err := roundTripper.RoundTrip(req)

    if err == nil && resp != nil {
        switch resp.StatusCode {
        case
            http.StatusMovedPermanently,
            http.StatusSeeOther,
            http.StatusFound,
            http.StatusTemporaryRedirect,
            http.StatusPermanentRedirect:
            if len(resp.Header.Get("Location")) == 0 {
                resp.StatusCode = 406
            }
        }
    }
    return resp, err
}
jeevatkm commented 1 year ago

@itziklavon You could make use of Redirect policy implementation.

https://pkg.go.dev/github.com/go-resty/resty/v2#readme-redirect-policy