golang常用的http请求操作
程序员文章站
2022-06-24 09:00:18
之前用python写各种网络请求的时候写的非常顺手,但是当打算用golang写的时候才发现相对来说还是python的那种方式用的更加顺手,习惯golang的用法之后也就差别不大了,下面主要整理了常用的通过golang发起的GET请求以及POST请求的代码例子 golang发起GET请求 基本的GET ......
之前用python写各种网络请求的时候写的非常顺手,但是当打算用golang写的时候才发现相对来说还是python的那种方式用的更加顺手,习惯golang的用法之后也就差别不大了,下面主要整理了常用的通过golang发起的get请求以及post请求的代码例子
golang发起get请求
基本的get请求
//基本的get请求 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.get("http://httpbin.org/get") if err != nil { fmt.println(err) return } defer resp.body.close() body, err := ioutil.readall(resp.body) fmt.println(string(body)) fmt.println(resp.statuscode) if resp.statuscode == 200 { fmt.println("ok") } }
带参数的get请求
package main import ( "fmt" "io/ioutil" "net/http" ) func main(){ resp, err := http.get("http://httpbin.org/get?name=zhaofan&age=23") if err != nil { fmt.println(err) return } defer resp.body.close() body, _ := ioutil.readall(resp.body) fmt.println(string(body)) }
但是如果我们想要把一些参数做成变量而不是直接放到url中怎么操作,代码例子如下:
package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main(){ params := url.values{} url, err := url.parse("http://httpbin.org/get") if err != nil { return } params.set("name","zhaofan") params.set("age","23") //如果参数中有中文参数,这个方法会进行urlencode url.rawquery = params.encode() urlpath := url.string() fmt.println(urlpath) // https://httpbin.org/get?age=23&name=zhaofan resp,err := http.get(urlpath) defer resp.body.close() body, _ := ioutil.readall(resp.body) fmt.println(string(body)) }
解析json类型的返回结果
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) type result struct { args string `json:"args"` headers map[string]string `json:"headers"` origin string `json:"origin"` url string `json:"url"` } func main() { resp, err := http.get("http://httpbin.org/get") if err != nil { return } defer resp.body.close() body, _ := ioutil.readall(resp.body) fmt.println(string(body)) var res result _ = json.unmarshal(body,&res) fmt.printf("%#v", res) }
get请求添加请求头
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { client := &http.client{} req,_ := http.newrequest("get","http://httpbin.org/get",nil) req.header.add("name","zhaofan") req.header.add("age","3") resp,_ := client.do(req) body, _ := ioutil.readall(resp.body) fmt.printf(string(body)) }
从上述的结果可以看出我们设置的头是成功了:
{ "args": {}, "headers": { "accept-encoding": "gzip", "age": "3", "host": "httpbin.org", "name": "zhaofan", "user-agent": "go-http-client/1.1" }, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/get" }
golang 发起post请求
基本的post使用
package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main() { urlvalues := url.values{} urlvalues.add("name","zhaofan") urlvalues.add("age","22") resp, _ := http.postform("http://httpbin.org/post",urlvalues) body, _ := ioutil.readall(resp.body) fmt.println(string(body)) }
结果如下:
{ "args": {}, "data": "", "files": {}, "form": { "age": "22", "name": "zhaofan" }, "headers": { "accept-encoding": "gzip", "content-length": "19", "content-type": "application/x-www-form-urlencoded", "host": "httpbin.org", "user-agent": "go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" }
另外一种方式
package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) func main() { urlvalues := url.values{ "name":{"zhaofan"}, "age":{"23"}, } reqbody:= urlvalues.encode() resp, _ := http.post("http://httpbin.org/post", "text/html",strings.newreader(reqbody)) body,_:= ioutil.readall(resp.body) fmt.println(string(body)) }
结果如下:
{ "args": {}, "data": "age=23&name=zhaofan", "files": {}, "form": {}, "headers": { "accept-encoding": "gzip", "content-length": "19", "content-type": "text/html", "host": "httpbin.org", "user-agent": "go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" }
发送json数据的post请求
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) func main() { client := &http.client{} data := make(map[string]interface{}) data["name"] = "zhaofan" data["age"] = "23" bytesdata, _ := json.marshal(data) req, _ := http.newrequest("post","http://httpbin.org/post",bytes.newreader(bytesdata)) resp, _ := client.do(req) body, _ := ioutil.readall(resp.body) fmt.println(string(body)) }
结果如下:
{ "args": {}, "data": "{\"age\":\"23\",\"name\":\"zhaofan\"}", "files": {}, "form": {}, "headers": { "accept-encoding": "gzip", "content-length": "29", "host": "httpbin.org", "user-agent": "go-http-client/1.1" }, "json": { "age": "23", "name": "zhaofan" }, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" }
不用client的post请求
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) func main() { data := make(map[string]interface{}) data["name"] = "zhaofan" data["age"] = "23" bytesdata, _ := json.marshal(data) resp, _ := http.post("http://httpbin.org/post","application/json", bytes.newreader(bytesdata)) body, _ := ioutil.readall(resp.body) fmt.println(string(body)) }
上一篇: jq each遍历数组或对象
下一篇: gif图片转换为jpg格式 java