Home > Article > Backend Development > How to set header keys and values using go package: shurcooL/graphql or hasura/go-graphql-client?
So I want to use shurcool or hasura go client (Go package) to query data from Graphql server through Go, but the data server needs to be like The "x-hasura-admin-secret" key and value are included in the request header.
There is no mention in either package documentation of how to do this (setting header keys and values), only how to set the access token.
The client provided by https://www.php.cn/link/b93f552915e01e40fb9b66d6fd114f7b has a withrequestmodifier
method. You can add a request header like this:
import ( "net/http" graphql "github.com/hasura/go-graphql-client" ) func gqlinit() { client := graphql.newclient("your graphql url here", nil) client = client.withrequestmodifier(func(r *http.request) { r.header.set("x-hasura-admin-secret", "secret") }) }
Viewhttps://www.php.cn/link/3a5f9129110203548b21c0e40e9cd7af and the related github lib, it looks like they want you to pass a *http.client
to add headers for you, you can do this:
import ( "net/http" graphql "github.com/shurcooL/graphql" ) type hasuraAuthTransport struct { secret string } func (h hasuraAuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { req.Header.Set("x-hasura-admin-secret", h.secret) return http.DefaultTransport.RoundTrip(req) } func gqlInit() { client := graphql.NewClient("your graphql url here", &http.Client{ Transport: hasuraAuthTransport{secret: "secret"}, }) }
The above is the detailed content of How to set header keys and values using go package: shurcooL/graphql or hasura/go-graphql-client?. For more information, please follow other related articles on the PHP Chinese website!