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

Curl在Insomnia正常运行,Postman、Mac终端及Python请求均失败求助

访问Lowes API时Postman、终端及Python请求均返回访问拒绝,仅Insomnia正常的问题

问题详情

我尝试在Postman、Insomnia及本地Mac终端中执行以下curl命令,仅Insomnia能返回有效的JSON响应,Postman与终端均返回访问拒绝错误。

执行的curl命令

curl --request GET \
  --url 'https://www.lowes.com/rnr/r/get-by-product/5013699701/pdp/prod?offset=10' \
  --header 'accept: application/json, text/plain, */*' \
  --header 'accept-language: en-US,en;q=0.9,hi;q=0.8,de;q=0.7,ur;q=0.6,pa;q=0.5,es;q=0.4' \
  --header 'authority: www.lowes.com' \
  --header 'dnt: 1' \
  --header 'referer: https://www.lowes.com/pd/Whirlpool-21-4-cu-ft-Side-By-Side-Refrigerator-with-Ice-and-Water-Dispenser-and-Can-Caddy-Fingerprint-Resistant-Stainless-Steel/5013699701' \
  --header 'sec-ch-ua: "Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"' \
  --header 'sec-ch-ua-mobile: ?0' \
  --header 'sec-ch-ua-platform: "macOS"' \
  --header 'sec-fetch-dest: empty' \
  --header 'sec-fetch-mode: cors' \
  --header 'sec-fetch-site: same-origin' \
  --header 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'

错误响应内容

Postman与Mac终端返回的错误如下:

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access &quot;http&#58;&#47;&#47;www&#46;lowes&#46;com&#47;rnr&#47;r&#47;get&#45;by&#45;product&#47;5013699701&#47;pdp&#47;prod&#63;&quot; on this server.<P>
Reference&#32;&#35;18&#46;b56e3f17&#46;1679172086&#46;19ace9d1
</BODY>
</HTML>

无法正常工作的Python请求代码

我编写的Python requests代码同样返回访问拒绝:

import requests

url = "https://www.lowes.com/rnr/r/get-by-product/5013699701/pdp/prod?offset=10"

payload={}
headers = {
  'accept': 'application/json, text/plain, */*',
  'accept-language': 'en-US,en;q=0.9,hi;q=0.8,de;q=0.7,ur;q=0.6,pa;q=0.5,es;q=0.4',
  'authority': 'www.lowes.com',
  'dnt': '1',
  'referer': 'https://www.lowes.com/pd/Whirlpool-21-4-cu-ft-Side-By-Side-Refrigerator-with-Ice-and-Water-Dispenser-and-Can-Caddy-Fingerprint-Resistant-Stainless-Steel/5013699701',
  'sec-ch-ua': '&quot;Chromium&quot;;v=&quot;110&quot;, &quot;Not A(Brand&quot;;v=&quot;24&quot;, &quot;Google Chrome&quot;;v=&quot;110&quot;',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '&quot;macOS&quot;',
  'sec-fetch-dest': 'empty',
  'sec-fetch-mode': 'cors',
  'sec-fetch-site': 'same-origin',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

可能的原因及解决方法

  • Cookie缺失:Insomnia可能自动保留了之前访问Lowes页面时获取的有效会话Cookie,而Postman、终端和Python请求未携带这些Cookie。Lowes的API依赖会话Cookie验证用户合法性。解决:从Insomnia的请求详情中复制Cookie,添加到其他工具的请求头里。
  • 请求头格式错误:检查sec-ch-ua这类头部的引号转义,原curl和Python代码里的&quot;是HTML转义字符,实际应该用双引号(在Python中要转义为\",或者用单引号包裹值)。比如Python中sec-ch-ua的正确写法应为'"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"'。
  • TLS配置差异:不同工具的TLS版本或加密套件可能不同,Lowes服务器可能拦截不符合要求的请求。可在curl中添加--tlsv1.2参数指定TLS版本,Python中可通过修改requests的适配参数调整。
  • 反爬机制检测:Lowes的反爬系统可能识别出非浏览器环境的请求,除了User-Agent,还需检查Accept-Encoding、Connection等头部是否齐全,或者是否需要先访问商品页面获取必要的令牌再调用API。
  • 请求顺序问题:需先访问对应商品页面,让服务器生成会话Cookie后,再调用API接口。Insomnia可能之前已加载过商品页面,自动携带了相关Cookie。

修改后的Python示例代码(添加Cookie)

import requests

url = "https://www.lowes.com/rnr/r/get-by-product/5013699701/pdp/prod?offset=10"

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'en-US,en;q=0.9,hi;q=0.8,de;q=0.7,ur;q=0.6,pa;q=0.5,es;q=0.4',
    'authority': 'www.lowes.com',
    'dnt': '1',
    'referer': 'https://www.lowes.com/pd/Whirlpool-21-4-cu-ft-Side-By-Side-Refrigerator-with-Ice-and-Water-Dispenser-and-Can-Caddy-Fingerprint-Resistant-Stainless-Steel/5013699701',
    'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
    # 替换为从Insomnia复制的实际Cookie
    'Cookie': 'lowes_session=xxxxxx; __Secure-PSID=xxxxxx; ...'
}

response = requests.get(url, headers=headers)
print(response.text)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 20:42:26