This is my first experience with go, I'm coming from a python background and I'm trying to run this post api(https://reqres.in/api/users) in parallel using goroutines with the different set of post data which is variable jsonStr in this code.
Any help much appreciated for running this api in paralle with the list of data
My data
data = [{"name": "bonny gaud", "movies": ["Terminator", "Transformer"], {"name": "Sarah palin", "movies": ["No country for old", "James Bond"] }
My Code:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
"bytes"
)
func main() {
start := time.Now()
url := "https://reqres.in/api/users"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
fmt.Print("Everything:", time.Since(start))
fmt.Print(string(body))
}
You can use waitGroup or errGroup. Here is the example :
package main
import (
"bytes"
"fmt"
"golang.org/x/sync/errgroup"
"io/ioutil"
"net/http"
"time"
)
func main() {
dataSets := []string{`{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`,
`{"name": "paul rudd", "movies": ["I Love You Man", "Role Models"] }`}
var eg errgroup.Group
client := http.DefaultClient
for _, jsonStr := range dataSets {
eg.Go(func() error {
return callClient(client, jsonStr)
})
}
if err := eg.Wait(); err != nil {
fmt.Printf("Encountered error: %v", err)
}
fmt.Println("Successfully finished.")
}
func callClient(client *http.Client, jsonStr string) error {
start := time.Now()
url := "https://reqres.in/api/users"
fmt.Println("URL:>", url)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(jsonStr)))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("send request: %w", err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
fmt.Println("response Body:", string(body))
fmt.Print("Everything:", time.Since(start))
fmt.Print(string(body))
return nil
}
go get golang.org/x/sync