Golang编程语言知识介绍


  • 首页

  • todo

  • 思考

  • life

  • food

  • OS

  • lua

  • redis

  • Golang

  • C

  • TCP/IP

  • ebpf

  • p4

  • OpenVPN

  • IPSec

  • L2TP

  • DNS

  • distributed

  • web

  • OpenWRT

  • 运维

  • Git

  • 鸟哥的私房菜

  • IT杂谈

  • 投资

  • About Me

  • 友情链接

  • FTP

  • 搜索
close

go post请求常用的几种方式

时间: 2021-04-03   |   分类: go     |   阅读: 144 字 ~1分钟

原文链接

post请求常用的几种方式,记录一下

func httpPost() {
    resp, err := http.Post("https://www.abcd123.top/api/v1/login",
        "application/x-www-form-urlencoded",
        strings.NewReader("username=test&password=ab123123"))
    if err != nil {
        fmt.Println(err)
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
 
    fmt.Println(string(body))
}
 
func httpPostForm() {
    resp, err := http.PostForm("https://www.denlery.top/api/v1/login",
        url.Values{"username": {"auto"}, "password": {"auto123123"}})
    if err != nil {
        // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
    fmt.Println(string(body))
 
}
 
func httpPostJson() {
    jsonStr :=[]byte(`{ "username": "auto", "password": "auto123123" }`)
    url:= "https://www.denlery.top/api/v1/login"
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")
 
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // handle error
    }
    defer resp.Body.Close()
 
    statuscode := resp.StatusCode
    hea := resp.Header
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
    fmt.Println(statuscode)
    fmt.Println(hea)
 
}
#go#
go系统学习推荐书籍
go中的init函数以及main函数
shankusu2017@gmail.com

shankusu2017@gmail.com

日志
分类
标签
GitHub
© 2009 - 2025
粤ICP备2021068940号-1 粤公网安备44011302003059
0%