目录
发送Get请求
响应信息
拓展
资料获取方法
发送Get请求
使用Golang发送get请求很容易,我们还是使用http://httpbin.org
作为服务端来进行演示。
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"net/url"
"reflect"
)
func main() {
resp, err := http.Get("http://httpbin.org/get?name=Detector")
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
headers := resp.Header
// headers 打印报文头部信息
for k, v := range headers {
fmt.Printf("%v, %v\n", k, v) // %v 打印interfac{}的值
}
// 打印响应信息内容
fmt.Printf("响应状态:%s,响应码: %d\n", resp.Status, resp.StatusCode)
fmt.Printf("协议:%s\n", resp.Proto)
fmt.Printf("响应内容长度: %d\n", resp.ContentLength)
fmt.Printf("编码格式:%v\n", resp.TransferEncoding) // 未指定时为空
fmt.Printf("是否压缩:%t\n", resp.Uncompressed)
fmt.Println(reflect.TypeOf(resp.Body)) // *http.gzipReader
fmt.Println(resp.Close)
buf := bytes.NewBuffer(make([]byte, 0, 512))
length, _ := buf.ReadFrom(resp.Body)
fmt.Println(len(buf.Bytes()))
fmt.Println(length)
fmt.Println(string(buf.Bytes()))
}
响应信息
λ go run goHttp.go
Access-Control-Allow-Credentials, [true]
Via, [1.1 vegur]
Connection, [keep-alive]
Server, [gunicorn/19.9.0]
Date, [Tue, 18 Sep 2018 02:25:16 GMT]
Content-Type, [application/json]
Content-Length, [271]
Access-Control-Allow-Origin, [*]
"响应状态":200 OK,响应码: 200
"协议":HTTP/1.1
"响应内容长度": 271
"编码格式":[]
"是否压缩":false
*http.bodyEOFSignal
false
271
271
{
"args": {
"name": "Detector"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1"
},
"origin": "14.152.49.250",
"url": "http://httpbin.org/get?name=Detector"
}
更多的响应内容我们查看安装路径的net包中Response struct 的信息,里面有详细的注释,参考路径:C:\Go\src\net\http\response.go
:
拓展
用过Python的同学都知道,Python的requests发送get请求时是可以直接传递字典的(dict/map)的,比如:
#! /usr/bin/python
import requests
a = {"name": "bingo", "age": "18", "location": "shenzhen"}
name = {"name": "Detector"}
resp = requests.get("http://httpbin.org/get", params=a)
print(resp.text)
响应信息:
{
"args": {
"age": "18",
"location": "shenzhen",
"name": "bingo"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"origin": "14.152.49.250",
"url": "http://httpbin.org/get?name=bingo&age=18&location=shenzhen"
}
那么在golang中有没有想过的方法呢,答案是有的,但是只有一个半成品。
在C:\Go\src\net\url\url.go
中我们可以看到一个结构体type Values map[string][]string
,他有一个Encode()方法可以把key:value键值对转化为key=value&key=value的形式:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func main() {
name := url.Values{"name": {"bingo"}, "age": {"18"}, "local": {"shenzhen"}}
param := name.Encode()
url := fmt.Sprintf("http://httpbin.org/get?%s", param)
fmt.Println(url)
resp, err := http.Get(url)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
输出:
http://httpbin.org/get?age=18&local=shenzhen&name=bingo
{
"args": {
"age": "18",
"local": "shenzhen",
"name": "bingo"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1"
},
"origin": "103.84.19.17",
"url": "http://httpbin.org/get?age=18&local=shenzhen&name=bingo"
}
考虑到篇幅,我们把发送Get、Post单独出来。我们在下一篇一起学习怎么发送Post请求。
资料获取方法
【留言777】
各位想获取源码等教程资料的朋友请点赞 + 评论 + 收藏,三连!
三连之后我会在评论区挨个私信发给你们~