-
Notifications
You must be signed in to change notification settings - Fork 282
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
Ability to add custom headers to the request #28
Comments
It is possible to do this by using a custom HTTP client (that sets custom request headers before delegating to the underlying HTTP client). See https://github.com/shurcooL/githubv4#authentication for more information. Please let me know if you're satisfied with that solution or if you have any additional questions. |
@dmitshur it's possible to do by creating your own HTTP Client, but it's a lot of work (just look at all the code in import "net/http"
type headerTransport struct {
base http.RoundTripper
headers map[string]string
}
func NewHTTPClientWithHeaders(baseRoundTripper http.RoundTripper, headers map[string]string) *http.Client {
if baseRoundTripper == nil {
baseRoundTripper = http.DefaultTransport
}
return &http.Client{
Transport: &headerTransport{
base: baseRoundTripper,
headers: headers,
},
}
}
func (h *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := CloneRequest(req)
for key, val := range h.headers {
req2.Header.Set(key, val)
}
return h.base.RoundTrip(req2)
}
// CloneRequest and CloneHeader copied from https://github.com/kubernetes/apimachinery/blob/master/pkg/util/net/http.go#L424
// CloneRequest creates a shallow copy of the request along with a deep copy of the Headers.
func CloneRequest(req *http.Request) *http.Request {
r := new(http.Request)
// shallow clone
*r = *req
// deep copy headers
r.Header = CloneHeader(req.Header)
return r
}
// CloneHeader creates a deep copy of an http.Header.
func CloneHeader(in http.Header) http.Header {
out := make(http.Header, len(in))
for key, values := range in {
newValues := make([]string, len(values))
copy(newValues, values)
out[key] = newValues
}
return out
} Perhaps |
The code in The code you posted is a much more appropriately sized for a customized solution for your app's needs, and I recommend you continue to use that. It works with
This would be quite inflexible: not everyone needs this, and it'd stop working for people as soon as they need slightly custom behavior.
This is more viable, but it needs to be thought out well before committing to it. If you'd like to think more about this, I suggest coming up with some drafts with mock APIs and user code that uses that API. I don't expect I'll have time to work on this soon. |
It seems that since #5 is desired, adding more custom HTTP handling will be tricky without implementing the transport-agnostic interface layer. I'd be very happy to pick up work on #5 if others have no desire any more.
It's perhaps appropriately sized, but the complexity that it entails is a bit much. You need to care and know about roundtrippers, and that they aren't supposed to mutate the request, but that a shallow-copy of the request and a deep copy of the headers is fine. |
How about the below signature?
It is backwards incompatible. However, clients can pass Full diff is given below.
|
This would be very useful for GitHub's Schema Previews, which need a custom media type in the Accept header. |
you can try my fork https://github.com/Laisky/graphql , support custom headers & cookies, and fully compatible with shurcool/graphql. |
With github deprecating the current authentication method [1] , its likely we would need to at least support the addition of [1] https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/ |
@gurpreetz Thanks for pointing out that GitHub is deprecating authentication via query parameters, and recommending authentication via request headers. However, I don't think that changes best practices for using this library. The |
Can you please elaborate? Can I reproduce this somehow? I've looked at https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql and I'm not seeing any changes to the authentication on GitHub's side beyond what I've already mentioned in my last comment. If you think there is an issue, please file a new issue in https://github.com/shurcooL/githubv4/issues with more information. |
This is basically what I have...
I then got an email from github stating that :
|
@gurpreetz Thanks. That should not be happening, I'll need to investigate. Can you please file a new issue at https://github.com/shurcooL/githubv4/issues and specify what version of |
Hi, I needed to set the authorization in the header for my graphql request and I found a solution that works for me. I need to provide a jwt token with my request for my backend: This is what I added: Full code:
Source: #77 |
@VanCoppenolleWout what is |
It is something like this func setAuthHeader(token string) graphql.RequestModifier {
return func(h *http.Request) {
h.Header.Add("Authorization", token)
}
} |
Ha, found this after crafting a similar approach. Used for connecting to AWS AppSync. I already have a package named func setHeader(header string, token string) graphql.RequestModifier {
return func(h *http.Request) {
h.Header.Add(header, token)
}
}
var gqlClient *graphql.Client
gqlClient = graphql.NewClient(endpointURL, &http.Client{}).WithRequestModifier(setHeader("x-api-key", apiKey)) |
Currently the only header being set is the content type:
/shurcooL/go/ctxhttp/ctxhttp.go:83
It would be helpful to allow setting custom headers in addition to the content-type since some graphql servers use headers for auth tokens and setting other variables.
Thanks
The text was updated successfully, but these errors were encountered: