Execute HTTP POST Request in Go

Posted by yhuang
Public (Editable by Users)
Go
Edit
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {

	resp, err := http.Post("https://mysite.com/endpoint", "text/plain", strings.NewReader("Content Body"))
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()
	fmt.Println(resp.Status)

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(body))
}