Skip to content

Instantly share code, notes, and snippets.

@vvakame vvakame/main.go Secret
Created Aug 9, 2018

Embed
What would you like to do?
GraphQL client by Go+stdlib demo
package main
import (
"net/http"
"bytes"
"io/ioutil"
"fmt"
"encoding/json"
"net/url"
"os"
)
func main() {
githubToken := os.Getenv("GITHUB_TOKEN")
query := `
{
viewer {
id
name
}
}
`
b, err := json.Marshal(struct {
Query string `json:"query"`
}{
Query: query,
})
if err != nil {
panic(err)
}
endpointURL, err := url.Parse("https://api.github.com/graphql")
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(b)
resp, err := http.DefaultClient.Do(&http.Request{
URL: endpointURL,
Method: "POST",
Header: http.Header{
"Content-Type": {"application/json"},
"Authorization": {"bearer " + githubToken},
},
Body: ioutil.NopCloser(buf),
})
if err != nil {
panic(err)
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
@vvakame

This comment has been minimized.

Copy link
Owner Author

commented Aug 9, 2018

You MUST exec with GITHUB_TOKE=<your personal access token place here>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.