与其他编程语言一样,golang也可以发送http请求,同样也有很多第三方库,本文介绍的是golang原生自带的http请求方式
1. 发送最简单的get请求,如下是访问百度http://www.baidu.com
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "http://www.baidu.com"
method := "GET"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
1. 发送请求上传文件
向飞书文档上传文件
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"bytes"
"mime/multipart"
"os"
"path/filepath"
)
var (
url = "https://internal-api-drive-stream.feishu.cn/space/api/box/stream/upload/all/?name=123.txt&size=6&checksum=70123830&mount_node_token=&mount_point=explorer&push_open_history_record=1&ext%255Bextra%255D=&size_checker=true"
path = "C:/Users/Lenovo/Desktop/123.txt"
cookie = "__tea__ug__uid=5947971649419715451; _ga=GA1.2.1006846727.1649419716; slardar_delay_type=b; _uuid_hera_ab_path_1=7108239728494772226; locale=zh-CN; trust_browser_id=bf70b95a-e143-4d7c-9d88-30a5945ec37e; Hm_lvt_e78c0cb1b97ef970304b53d2097845fd=1655016032; Hm_lpvt_e78c0cb1b97ef970304b53d2097845fd=1655016038; passport_web_did=7109985771645304835; landing_url=https://login.feishu.cn/accounts/page/login?app_id=2&query_scope=all&redi3rect_uri=https%3A%2F%2Fgg6xi50yvf.feishu.cn%2Fdocs%2FdoccnjtSAS1m3gu2F7Us5x5IOpb%3Flogin_redirect_times%3D1; is_anonymous_session=; _csrf_token=e81b6dabc41d21c76f3082d965f4ee91691a0465-1655422581; lang=zh; session=XN0YXJ0-af7q32ec77-1efb-4f00-b986-691e0df6d6b23b-WVuZA; session_list=XN0YXJ0-af7qec77-1efb-4f00-b986-691e0df6d6bb-WVuZA_XN0YXJ0-5f7g7ce2-7dddeb-4652-8107-94b9e3ha12h3-WVuZA_XN0YXJ0-af7qec77-1efb-4f00-b986-691e0df6d6bb-WVuZA; swp_csrf_token=a6bec888-df2c-4d90-8cd1-f3fd95263ab4; t_beda37=9d977c39fcf210832b015fc1e8474c9922c1d88ec15748a6d435fdf95fb21ac6d"
)
func main(){
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, err := os.Open(path)
defer file.Close()
part, err := writer.CreateFormFile("file", filepath.Base(path))
_, err = io.Copy(part, file)
if err != nil {
fmt.Println("err1 = ", err)
return
}
err = writer.Close()
if err != nil {
fmt.Println("err2 = ", err)
return
}
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println("err3 = ", err)
return
}
req.Header.Add("Cookie", cookie)
req.Header.Add("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req)
if err != nil {
fmt.Println("err4 = ", err)
return
}
defer req.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("err5 = ", err)
return
}
fmt.Println(string(body))
}
结果:
{"code":0,"message":"Success","data":{"data_version":"7110367276871745537","extra":{"node_token":"nodcnr1MrFTFxYlAJ89ELlSnhV6"},"file_token":"boxcnDrUM1wNigYHn6p0Cwq3rEY","version":"7110367276871745537"}}
上传成功
3. x-www-form-urlencoded类型的请求
类似于postman这样的
package main
import (
"fmt"
"net/http"
"io/ioutil"
"strings"
)
func main(){
url := "http://localhost:8080/form"
method := "POST"
payload := strings.NewReader("name=10&password=12345")
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println("err1 = ", err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("err2 = ", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("err3 = ", err)
return
}
fmt.Println(string(body))
}
结果:
username:10,password:12345,type:post