golang发送post请求

Golang发送post请求

   post := "{"待发送":"json"}" fmt.Println(post) var jsonstr = []byte(post) //转换二进制 buffer:= bytes.NewBuffer(jsonstr) request, err := http.NewRequest("POST", api_url, buffer) if err != nil { fmt.Printf("http.NewRequest%v", err) return queryobj, err } request.Header.Set("Content-Type", "application/json;charset=UTF-8") //添加请求头 client := http.Client{} //创建客户端 resp, err := client.Do(request.WithContext(context.TODO())) //发送请求 if err != nil { fmt.Printf("client.Do%v", err) return queryobj, err } respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Printf("ioutil.ReadAll%v", err) return queryobj, err }
  

 

posted @ 2019-06-06 18:52  tymagic  阅读(14300)  评论(0编辑  收藏  举报