You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
57 lines
1.4 KiB
package qweather_test
|
|
|
|
import (
|
|
"yunyan/sys/qweather"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func Test_api(t *testing.T) {
|
|
// 请求的URL
|
|
url := "https://mf6apvd72m.re.qweatherapi.com/geo/v2/city/lookup?location=北京"
|
|
|
|
// 创建一个新的HTTP请求
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
fmt.Println("创建请求失败:", err)
|
|
return
|
|
}
|
|
|
|
// 设置请求头,包含API密钥
|
|
req.Header.Set("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
|
|
req.Header.Set("X-QW-Api-Key", "525e6198b9f846608dbfae43758ff8c4")
|
|
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
|
|
|
|
// 创建一个HTTP客户端并发送请求
|
|
client := &http.Client{}
|
|
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
|
|
}
|
|
|
|
// 打印响应状态码和响应体
|
|
fmt.Printf("响应状态码: %d\n", resp.StatusCode)
|
|
fmt.Printf("响应体: %s\n", body)
|
|
}
|
|
func Test_Sys_GeoApi(t *testing.T) {
|
|
if sys, err := qweather.NewSys(
|
|
qweather.SetApiHost("mf6apvd72m.re.qweatherapi.com"),
|
|
qweather.SetApiKey("525e6198b9f846608dbfae43758ff8c4"),
|
|
); err != nil {
|
|
fmt.Printf("Sys Init err:%v", err)
|
|
} else {
|
|
result, err := sys.GeoApi("北京")
|
|
fmt.Printf("result:%v err:%v", result, err)
|
|
}
|
|
}
|
|
|