用Go net/http发送Restful请求

https://www.jianshu.com/p/bd6fc894e7be

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net/http"
)
func main() {
    url := "http://127.0.0.1:5000/api/version/resources/resource_item"
    fmt.Println("URL:>", url)

  // 使用转义反引号完成json转换
    item := "testKey"
    updateParams := `{"testKey":"` + item + `"}`

    var jsonStr = []byte(updateParams)

    req, _ := http.NewRequest(http.MethodPatch, url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, _ := client.Do(req)

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}
发布了397 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/sun007700/article/details/104199291