Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created July 4, 2017 20:38
Show Gist options
  • Save gjyoung1974/b313c602766e950df58bcc5127178b6e to your computer and use it in GitHub Desktop.
Save gjyoung1974/b313c602766e950df58bcc5127178b6e to your computer and use it in GitHub Desktop.
gloang http post
package main
import (
"fmt"
"bytes"
"io/ioutil"
"net/http"
)
func main() {
url := "http://httpbin.org/post"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"string":"Some sentence goes here."}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "some_value")
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment