Skip to content

Instantly share code, notes, and snippets.

@cuongld2

cuongld2/main.go

Created Dec 11, 2020
Embed
What would you like to do?
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