New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we set a User-Agent header? #911

Closed
thommahoney opened this Issue Feb 1, 2017 · 11 comments

Comments

Projects
None yet
7 participants
@thommahoney
Copy link

thommahoney commented Feb 1, 2017

Problem statement

We would like to be able to set an arbitrary user-agent header either at client generation time or at compile time. Is it possible to do this?

Swagger specification

The Swagger specification is irrelevant in this case because we are using the same specification to generate many clients.

@casualjim casualjim added the question label Feb 2, 2017

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Feb 2, 2017

You can use a custom transport which allows you to set the user agent.
https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L132

And you can configure it with this constructor method
https://github.com/go-swagger/go-swagger/blob/master/examples/todo-list/client/todo_list_client.go#L52

You can also configure that runtime with a stdlib http.Client
https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L167

@thommahoney

This comment has been minimized.

Copy link

thommahoney commented Feb 7, 2017

If I understand correctly, you're saying that we ought to write our own Submit() function (defined here) that implements the runtime.ClientTransport interface and configure our client to use that instead using the New() function?

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Feb 8, 2017

You can extend intercept a http request with the http.RoundTripper interface. https://godoc.org/net/http#RoundTripper

which you can set here: https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L116
so for the client here:

var myRoundTripper http.RoundTripper = createRoundTripper()
transport := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
transport.Transport = myRoundTripper
todoListClient := New(transport, nil)
@thommahoney

This comment has been minimized.

Copy link

thommahoney commented Feb 16, 2017

All set here. Thanks. Feel free to close this issue.

@GlenDC GlenDC closed this Feb 16, 2017

@casualjim casualjim reopened this Feb 16, 2017

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Feb 16, 2017

reopened so people can find it easily

@polds

This comment has been minimized.

Copy link

polds commented Feb 17, 2017

Can the same pattern of using an http.RoundTripper be used to implement the AWS Signature v4 which requires reading and modifying the *http.Request before its sent?

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Feb 17, 2017

yes it can, the roundtripper is the last thing executed before sending the request on the wire

@polds

This comment has been minimized.

Copy link

polds commented Feb 17, 2017

Indeed it can, thank you.

@Sef1995

This comment has been minimized.

Copy link

Sef1995 commented Oct 5, 2017

Is it possible to do this without a http.RoundTripper? I'm using gock for mocking the request in a test. But the test won't work because the roundtripper var is already set. See; h2non/gock#27

@fredbi fredbi referenced this issue Dec 23, 2017

Merged

A FAQ md to recap questions #1342

4 of 4 tasks complete

@fredbi fredbi closed this in #1342 Dec 23, 2017

@sachnk

This comment has been minimized.

Copy link

sachnk commented Dec 15, 2018

@Sef1995 Did you ever get gock working with go-swagger? I'm running into the same issue -- I can't figure out how to get gock to intercept my traffic that originates from my client generated code.

@fredbi

This comment has been minimized.

Copy link
Contributor

fredbi commented Dec 15, 2018

@sachnk we don't follow up closed issues. If the above answers do not address your expectations, I'd suggest you create a new issue in which you describe your use case in details. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment