resty icon indicating copy to clipboard operation
resty copied to clipboard

Unable to send custom header

Open itziklavon opened this issue 1 year ago • 1 comments

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
}

itziklavon avatar Mar 10 '23 09:03 itziklavon

@itziklavon You could make use of Redirect policy implementation.

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

jeevatkm avatar Sep 17 '23 09:09 jeevatkm