Skip to content

Instantly share code, notes, and snippets.



Created Dec 11, 2020
What would you like to do?
GraphQL request in Go
package main
import (
func main() {
file, err := os.Open("resources/collected_queries.txt")
if err != nil {
log.Fatalf("failed to open")
// The bufio.NewScanner() function is called in which the
// object os.File passed as its parameter and this returns a
// object bufio.Scanner which is further used on the
// bufio.Scanner.Split() method.
scanner := bufio.NewScanner(file)
// The bufio.ScanLines is used as an
// input to the method bufio.Scanner.Split()
// and then the scanning forwards to each
// new line using the bufio.Scanner.Scan()
// method.
for scanner.Scan() {
time.Sleep(2 * time.Second)
request, err := http.NewRequest("POST", "https://{host}/endpoint", bytes.NewBuffer([]byte(scanner.Text())))
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Do(request)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
data, _ := ioutil.ReadAll(response.Body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment