Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created December 11, 2020 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/2b2e7af80b8219744b39d549c9b6d54b to your computer and use it in GitHub Desktop.
Save cuongld2/2b2e7af80b8219744b39d549c9b6d54b to your computer and use it in GitHub Desktop.
GraphQL request in Go
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
)
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.
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
time.Sleep(2 * time.Second)
request, err := http.NewRequest("POST", "https://{host}/endpoint", bytes.NewBuffer([]byte(scanner.Text())))
request.Header.Set("Content-Type","application/json")
request.Header.Set("Host",{host})
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)
response.Body.Close()
fmt.Println(string(data))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment