Skip to content

Instantly share code, notes, and snippets.

@vvakame

vvakame/main.go Secret

Created August 9, 2018 11:21
Show Gist options
  • Save vvakame/aaaa632bae97ee06c701f339fbae4dbc to your computer and use it in GitHub Desktop.
Save vvakame/aaaa632bae97ee06c701f339fbae4dbc to your computer and use it in GitHub Desktop.
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
Copy link
Author

vvakame 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