目次

サンプルコード

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))
}

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2021-01-03 (日) 15:45:46 (1207d)