0%

http 请求及响应解析

net/http 提供了 HTTP 客户端及服务器的实现。

Get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import (
"fmt"
"io/ioutil"
"net/http"
)

func main() {
resp, err := http.Get("https://httpbin.org/get")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode > 299 {
fmt.Printf("Response failed with status code: %d and\nbody: %s\n", resp.StatusCode, body)
return
}
fmt.Println(string(body))
}

PostForm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)

func main() {
params := url.Values{}
params.Set("param1", "value1")
params.Set("param2", "value2")

resp, err := http.PostForm("https://httpbin.org/post", params)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode > 299 {
fmt.Printf("Response failed with status code: %d and\nbody: %s\n", resp.StatusCode, body)
return
}
fmt.Println(string(body))
}

Complex Post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func main() {
params, _ := json.Marshal(map[string]interface{}{
"param1": "value1",
"param2": "value2",
})

client := &http.Client{}
req, err := http.NewRequest("POST", "https://httpbin.org/post", bytes.NewReader(params))
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode > 299 {
fmt.Printf("Response failed with status code: %d and\nbody: %s\n", resp.StatusCode, body)
return
}
fmt.Println(string(body))
}