HTTP请求示例
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
apiUrl := "https://data.infoway.io/stock/batch_kline/1/10/002594.SZ%2C00285.HK%2CTSLA.US"
// 创建HTTP客户端
client := &http.Client{}
// 创建GET请求
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// 设置请求头
req.Header.Set("User-Agent", "Mozilla/5.0")
req.Header.Set("Accept", "application/json")
req.Header.Set("apiKey", "yourApikey")
// 发送请求
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// 读取响应内容
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
// 输出结果
fmt.Printf("HTTP code:", resp.StatusCode)
fmt.Printf("message:", string(body))
}
Last updated