Wikicodia

The ctrl+c ctrl+v paradise!

User Tools

Site Tools


go:http_json_post_request

HTTP JSON POST request

To make an HTTP JSON POST request:

url := "http://example.com"

var jsonStr = []byte(`{"message":"hello!"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
	log.Fatalln(err)
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

sb := string(body)
log.Printf(sb)
go/http_json_post_request.txt · Last modified: 2022/08/04 21:10 by zod

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki