go http 请求

client

比较简单的方式有两个:(下面的代码是抄的)
1.http.post() / http.get()

func httpPost() {
    resp, err := http.Post("http://www.01happy.com/demo/accept.php",
        "application/x-www-form-urlencoded",
        strings.NewReader("name=cjb"))
    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))
}
  1. client.Do()
func httpDo() {
    client := &http.Client{}
 
    req, err := http.NewRequest("POST", "http://www.01happy.com/demo/accept.php", strings.NewReader("name=cjb")) //bytes.NewReader()
    if err != nil {
        // handle error
    }
 
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Cookie", "name=anny")
 
    resp, err := client.Do(req)
 
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
 
    fmt.Println(string(body))
}
  1. query 1
q := url.Values{}
q.Set("username", "user")  
q.Set("password", "passwd")  
data := q.Encode()

url := "http://localhost"
reqest, err := http.NewRequest("POST", url, strings.NewReader(data))
u, _ := url.Parse("http://localhost")  
q := u.Query()
// q := url.Values{} //also work
q.Set("username", "user")  
q.Set("password", "passwd")  
u.RawQuery = q.Encode()  

res, err := http.Get(u.String());  
q :=request.URL.Query()
//q := url.Values{} //also work
q.Set("username", "user")  
q.Set("password", "passwd")  
request.URL.RawQuery=q.Encode()

response, _ := client.Do(request)
  1. header
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
  1. 不确定返回body的数据时如何解析2
result, _:= ioutil.ReadAll(r.Body)  
r.Body.Close()  
fmt.Printf("%s\n", result)  

//未知类型的推荐处理方法  

var f interface{}  
json.Unmarshal(result, &f)   
m := f.(map[string]interface{})  
for k, v := range m {  
	switch vv := v.(type) {  
		case string:  
			fmt.Println(k, "is string", vv)  
		case int:  
			fmt.Println(k, "is int", vv)  
		case float64:  
			fmt.Println(k,"is float64",vv)  
		case []interface{}:  
			fmt.Println(k, "is an array:")  
			for i, u := range vv {  
				fmt.Println(i, u)  
			}  
		default:  
			fmt.Println(k, "is of a type I don't know how to handle")   
	}  
}  

server

//url 中的参数
r.ParseForm()
r.FormValue("key")

//from data 参数
r.ParseMultipartForm(1<<10)
r.Form.Get("key")
// 或者下面这样:
mReader := multipart.NewReader(r.Body, boundary)
for {
	var part *multipart.Part
	part, err = mReader.NextPart()
	if err == io.EOF{
		break;
	}

参考:
这两篇都不太好,不过例子可以跑通。
https://www.cnblogs.com/mafeng/p/7068837.html
https://studygolang.com/articles/4830


  1. https://stackoverflow.com/questions/30652577/go-doing-a-get-request-and-building-the-querystring/30657518 ↩︎

  2. https://blog.csdn.net/typ2004/article/details/38669949 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值