Created
July 4, 2017 20:38
-
-
Save gjyoung1974/b313c602766e950df58bcc5127178b6e to your computer and use it in GitHub Desktop.
gloang http post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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