这是我第一次使用 go,我来自 python 背景,我正在尝试使用具有不同帖子数据集的 goroutine 并行运行这个帖子 api( https://reqres.in/api/users )这是此代码中的变量 jsonStr 。
非常感谢您与数据列表并行运行此 api 的任何帮助
我的数据
data = [{"name": "bonny gaud", "movies": ["Terminator", "Transformer"], {"name": "Sarah Palin", "movies": ["No country for old", "占士邦”] }
我的代码:
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))
}
幕布斯7119047
相关分类