我正在try 使用Golang net/http
从API获取数据.当我从VS代码甚至postman 中使用雷霆客户端时,我得到了正确的数据,但当我试图从Golang代码中获取数据时,我得到了一个空响应.
数据获取分两步进行:
- 使用初始GET请求获取Cookie(此部分在两者中都工作得很好)
- 使用Cookie发出另一个获取所需数据的GET请求.(这是在Golang中给出空白响应的步骤,并在下面给出的postman 链接中命名为历史数据)
这是Golang的代码.代码可能有点长,但这只是因为添加了多行标题.
var BaseURL string = "https://www.nseindia.com"
func ReqConfig() *http.Request {
req, _ := http.NewRequest("GET", BaseURL, nil)
req.Header.Add("Accept", "*/*")
req.Header.Add("Accept-Encoding", "gzip, deflate, br")
req.Header.Add("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("Host", "www.nseindia.com")
req.Header.Add("Referer", "https://www.nseindia.com/get-quotes/equity")
req.Header.Add("X-Requested-With", "XMLHttpRequest")
req.Header.Add("sec-fetch-dest", "empty")
req.Header.Add("sec-fetch-mode", "cors")
req.Header.Add("pragma", "no-cache")
req.Header.Add("sec-fetch-site", "same-origin")
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36")
fmt.Println(1, req.Header.Get("Cookie"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
for _, cookie := range res.Cookies() {
req.AddCookie(cookie)
}
// TODO: Remove the need to call this API twice. This is just a temporary fix.
res, err = http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
for _, cookie := range res.Cookies() {
req.AddCookie(cookie)
}
cookies := req.Cookies()
for i := 0; i < len(cookies); i++ {
for j := i + 1; j < len(cookies); j++ {
if cookies[i].Name == cookies[j].Name {
cookies = append(cookies[:j], cookies[j+1:]...)
j--
}
}
}
req.Header.Del("Cookie")
for _, cookie := range cookies {
req.AddCookie(cookie)
}
fmt.Println("Fetched cookies")
return req
}
func HistoricalEQ(symbol string, from string, to string, series string) {
req := ReqConfig()
query := req.URL.Query()
query.Add("symbol", symbol)
query.Add("from", from)
query.Add("to", to)
query.Add("series", "[\""+series+"\"]")
req.URL.RawQuery = query.Encode()
req.URL.Path = "/api/historical/cm/equity"
client := &http.Client{Timeout: 40 * time.Second}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var data map[string]interface{}
json.NewDecoder(res.Body).Decode(&data)
// Prints `map[]` and not the whole json data which is provided in Postman req
fmt.Println(data)
}
func main() {
symbol := "PAYTM"
series := "EQ"
from_date := time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local).Format("02-01-2006")
to_date := time.Date(2023, 1, 24, 0, 0, 0, 0, time.Local).Format("02-01-2006")
HistoricalEQ(symbol, from_date, to_date, series)
}
如果您能够从Golang only中的其他方式从 GET https://www.nseindia.com/api/historical/cm/equity?symbol=PAYTM&series=[%22EQ%22]&from=28-12-2022&to=28-01-2023
中获取数据,那么也可以解决我的问题.你可以拨打https://www.nseindia.com/get-quotes/equity?symbol=PAYTM
查看网站前台.我请求GET请求可以通过转到历史数据选项卡并单击筛选器按钮来触发
与PYTHON类似的代码:https://github.com/jugaad-py/jugaad-data/blob/47bbf1aa39ebec3a260579c76ff427ea06e42acd/jugaad_data/nse/history.py#L61