目次 †
サンプルコード †
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
const url string = "http://localhost/api/sample"
func main() {
client := &http.Client{}
reqJson := "{\"param1\": \"abc\"}"
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(reqJson)))
req.Header.Set("Content-Type" , "application/json")
req.Header.Set("My-Header" , "Test")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
defer resp.Body.Close()
fmt.Printf("status : %d\n", resp.StatusCode)
fmt.Printf("length : %d\n", resp.ContentLength)
for k, v := range resp.Header {
fmt.Printf("header %s = %v\n",k, v)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("error: %T\n", err)
}
fmt.Printf("body: %v\n", string(body))
}