http包
http包
1 post请求
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "http://127.0.0.1:8080/api/v1/login"
method := "POST"
payload := strings.NewReader(`{
"userid":123456,
"password":"123xxx"
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
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))
}
2 get请求
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "http://127.0.0.1:8080/api/v1/get_id/?userid=94372175745650688"
method := "GET"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("token", "xxx")
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))
}
3 POST请求--万能封装请求函数
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func MyPost(url string, header, data map[string]interface{}) (resp string, err error) {
client := &http.Client{}
dataS, _ := json.Marshal(data)
dataR := strings.NewReader(string(dataS))
req, err := http.NewRequest("POST", url, dataR)
if err != nil {
return "", err
}
for k, v := range header {
req.Header.Add(k, v.(string))
}
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func main() {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
url := "http://127.0.0.1/user/list"
header := map[string]interface{}{
"Content-Type": "application/json",
"Authorization": token,
}
data := map[string]interface{}{
"page": 1,
"limit": 20,
}
res, err := MyPost(url, header, data)
if err != nil {
return
}
fmt.Println(res)
}
4 POST from表单请求--万能封装请求函数
func PostFrom(url string, header, data map[string]interface{}) (resp string, err error) {
formData := netUrl.Values{}
for k, v := range data {
formData.Set(k, v.(string))
}
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(formData.Encode()))
if err != nil {
return "", err
}
response, err := client.Do(req)
if err != nil {
return "", err
}
defer response.Body.Close()
for k, v := range header {
req.Header.Set(k, v.(string))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(body), nil
}
5 Get请求--万能封装请求函数
func Get(url string, header map[string]interface{}) (resp string, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
for k, v := range header {
req.Header.Add(k, v.(string))
}
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(body), nil
}
选择了IT,必定终身学习