57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetRequest(t *testing.T) {
|
|
http := HttpRequest{}
|
|
_, err := http.Request("GET", "https://www.baidu.com", nil).ParseBytes()
|
|
if err != nil {
|
|
t.Error("请求失败")
|
|
}
|
|
|
|
}
|
|
|
|
func ExampleRequest() {
|
|
http := HttpRequest{}
|
|
// You can define map directly or define a structure corresponding to the return value to receive data
|
|
var resp map[string]any
|
|
err := http.Request("GET", "http://127.0.0.1:9999/api/v1/hello-world?name=world", nil).ParseJson(&resp)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("%#v", resp)
|
|
}
|
|
|
|
func TestExampleRequest2(t *testing.T) {
|
|
http := HttpRequest{}
|
|
|
|
// You can define map directly or define a structure corresponding to the return value to receive data
|
|
|
|
var form = map[string]interface{}{
|
|
"infoMap": map[string]string{
|
|
"serialno": "125678789",
|
|
},
|
|
"oneLevelCategory": "asset_msg",
|
|
"twoLevelCategory": 102,
|
|
}
|
|
|
|
body, _ := json.Marshal(form)
|
|
|
|
var header = map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl9hcHBfaWQiOiIxNjUxMTY5MDAxNjU5ODA1Njk3Iiwic3ViIjoiSW5zcHVyLUF1dGgtTWFuYWdlciIsImxvZ2luX3VpZCI6IjE2NTExNjkwMDM0NTQ5Njc4MTAiLCJsb2dpbl9hY2NvdW50X2lkIjoiMTY1MTE2OTAwMzU1NTYzMTEwNSIsImlzcyI6Ikluc3B1ciIsImxvZ2luX2xvZ2lubmFtZSI6ImFkbWluIiwibG9naW5fYWNjb3VudF9uYW1lIjoiYWRtaW4iLCJ1c2Vyc19hcHBfaWQiOiIxNjUxMTY5MDAxNjU5ODA1Njk3IiwianRpIjoiMS4wIiwibG9naW5fdW5hbWUiOiLnrqHnkIblkZgiLCJleHAiOjQzMTUyMjIwMjAsIm5iZiI6MTcyNTYxMTAxMH0.auTYbL66l_BKH_PnqusOdI9MBp3g--ZUKfIes5pCoH2hWj7WnOAdp7PAkUZ-ow1LDGliPxwST8pWvcea2BNwug",
|
|
}
|
|
|
|
var resp map[string]any
|
|
err := http.Request("POST", "https://10.110.63.139:8082/cpn/api/plt/v1/msgSub/sendMsg", bytes.NewReader(body), header).ParseJson(&resp)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("%#v", resp)
|
|
}
|