You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang调用NSE API返回空数据,Postman/Thunder Client可正常获取

问题分析与解决方案

核心问题

你遇到的问题本质是Golang请求缺少必要的请求头,或Cookie传递方式不符合NSE API的校验规则。Postman/Thunder Client会自动带上浏览器风格的请求头并完整管理Cookie,而Golang默认的http请求可能缺失这些关键信息,导致API返回空响应。

关键修复点

  • 启用自动Cookie管理:不要手动提取和设置Cookie,使用http.CookieJar让http.Client自动处理Cookie的获取和传递,和Python的requests.Session逻辑一致。
  • 补全必要请求头:NSE的API会校验请求头,必须带上User-Agent、Accept、Referer等模拟浏览器的头信息,否则会被拦截或返回空。

修正后的Golang代码

package main

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"net/http/cookiejar"
	"net/url"
)

func main() {
	// 初始化带CookieJar的http.Client,自动管理Cookie
	jar, err := cookiejar.New(nil)
	if err != nil {
		fmt.Printf("创建CookieJar失败: %v\n", err)
		return
	}

	// 禁用TLS证书校验(若NSE证书存在问题,可选开启)
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	client := &http.Client{
		Jar:       jar,
		Transport: tr,
	}

	// 第一步:获取初始Cookie
	initialURL := "https://www.nseindia.com/"
	req, err := http.NewRequest("GET", initialURL, nil)
	if err != nil {
		fmt.Printf("创建初始请求失败: %v\n", err)
		return
	}

	// 带上浏览器风格请求头
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
	req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
	req.Header.Set("Referer", "https://www.nseindia.com/")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("初始请求失败: %v\n", err)
		return
	}
	defer resp.Body.Close()

	// 第二步:请求历史数据API
	historicalURL := "https://www.nseindia.com/api/historical/cm/equity?symbol=RELIANCE&series=[%22EQ%22]&from=01-01-2024&to=31-01-2024"
	historicalReq, err := http.NewRequest("GET", historicalURL, nil)
	if err != nil {
		fmt.Printf("创建历史数据请求失败: %v\n", err)
		return
	}

	// 重复带上必要请求头,保持与初始请求一致
	historicalReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
	historicalReq.Header.Set("Accept", "application/json, text/plain, */*")
	historicalReq.Header.Set("Referer", "https://www.nseindia.com/get-quotes/equity?symbol=RELIANCE")
	historicalReq.Header.Set("X-Requested-With", "XMLHttpRequest") // 模拟AJAX请求的关键头

	historicalResp, err := client.Do(historicalReq)
	if err != nil {
		fmt.Printf("历史数据请求失败: %v\n", err)
		return
	}
	defer historicalResp.Body.Close()

	// 读取响应内容
	buf := make([]byte, 1024*1024)
	n, err := historicalResp.Body.Read(buf)
	if err != nil && err.Error() != "EOF" {
		fmt.Printf("读取响应失败: %v\n", err)
		return
	}

	fmt.Printf("响应状态码: %d\n", historicalResp.StatusCode)
	fmt.Printf("响应内容:\n%s\n", string(buf[:n]))
}

对比Python示例的差异

Python的requests.Session会自动完成以下操作,而Golang的http.Client默认不会:

  • 自动管理所有Cookie(包括Session Cookie)
  • 自动保留请求头的一致性
    因此需要手动配置CookieJar和请求头,对齐Python的请求逻辑。

额外注意事项

  • 尽量使用真实浏览器的User-Agent字符串,避免被NSE的反爬机制拦截。
  • 确保请求URL的参数(比如日期格式、symbol编码)和Postman中的完全一致。
  • 部分NSE API会校验X-Requested-With头,必须设置为XMLHttpRequest来模拟AJAX请求。

内容的提问来源于stack exchange,提问作者Krushnal Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 14:45:28