I would like to define a http.Client
that automatically appends a form value to all GET/POST requests.
I naively tried implementing http.RoundTripper
as copy/pasted from another library uses this technique to modify headers for every request.
type Transport struct {
// Transport is the HTTP transport to use when making requests.
// It will default to http.DefaultTransport if nil.
// (It should never be an oauth.Transport.)
Transport http.RoundTripper
}
// Client returns an *http.Client that makes OAuth-authenticated requests.
func (t *Transport) Client() *http.Client {
return &http.Client{Transport: t}
}
func (t *Transport) transport() http.RoundTripper {
if t.Transport != nil {
return t.Transport
}
return http.DefaultTransport
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
// To set the Authorization header, we must make a copy of the Request
// so that we don't modify the Request we were given.
// This is required by the specification of http.RoundTripper.
req = cloneRequest(req)
>> req.Form.Set("foo", bar)
// Make the HTTP request.
return t.transport().RoundTrip(req)
}
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
// shallow copy of the struct
r2 := new(http.Request)
*r2 = *r
// deep copy of the Header
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}
return r2
}
This however does not work. The req.Form
values map doesn't seem to exist at this stage, so I get the panic:
panic: runtime error: assignment to entry in nil map
I tried adding this to the (t *Transport) RoundTrip
, but no luck:
err := req.ParseForm()
misc.PanicIf(err)
I've no idea what I'm doing, any tips?
EDIT: There is no point in trying to copy req.Form
values in cloneRequest
method, since r.Form
is empty map anyway.