[Go]GO语言实现发送post请求

//Post("http://xxxx","application/json;charset=utf-8",[]byte("{'aaa':'bbb'}"))
func Post(url string, contentType string, body []byte) (string, error) {
    res, err := http.Post(url, contentType, strings.NewReader(string(body)))
    if err != nil {
        return "", err
    }
    defer res.Body.Close()
    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        return "", err
    }
    return string(content), nil
}

发送一个JSON类型的请求

Post("http://xxxx","application/json;charset=utf-8",[]byte("{'aaa':'bbb'}"))

 

posted @ 2021-02-08 17:39  陶士涵  阅读(33)  评论(0编辑  收藏