CURL转Google Apps Script后startRow/endRow参数失效求助
解决Google Apps Script中startRow与endRow参数失效问题
你的startRow和endRow参数失效,核心原因有三点:
- 请求体(payload)缺失原CURL中携带的
filterModel和sortModel关键参数,接口需完整请求上下文才能解析分页参数 - 未显式指定请求方法为POST,UrlFetchApp默认使用GET请求,无法正确提交POST格式的请求体
- URL未完整拼接原CURL中的
d1、d2、path查询参数
修复后的完整代码
const token = "5edfe8277ff2a5.941835169784f269a9244f13350db36eedb814d1"; // 对俄文路径进行URL编码,避免乱码 const targetPath = encodeURIComponent("Женщинам/Одежда"); const requestUrl = `http://mpstats.io/api/wb/get/category?d1=2020-07-13&d2=2020-08-11&path=${targetPath}`; const requestOptions = { // 显式指定POST请求方法 method: "POST", headers: { 'X-Mpstats-TOKEN': token, 'Content-Type': 'application/json' }, // 完整还原原CURL的请求体参数 payload: JSON.stringify({ "startRow": 0, "endRow": 10, "filterModel": {"id":{"filterType":"number","type":"equals","filter":13495594,"filterTo":null}}, "sortModel": [{"colId":"revenue","sort":"desc"}] }) }; // 执行请求并解析响应 const response = UrlFetchApp.fetch(requestUrl, requestOptions); const responseData = JSON.parse(response.getContentText()); console.log(responseData);
关键修复点说明
- 补全请求体参数:原CURL的
data-raw包含filterModel和sortModel,接口依赖这些参数确定数据筛选、排序规则,分页参数需配合这些规则才能生效 - 指定POST方法:UrlFetchApp默认使用GET,必须显式声明
method: "POST"才能正确提交JSON格式的请求体 - URL编码路径:原路径中的俄文需用
encodeURIComponent编码,避免URL解析错误导致请求失败
内容的提问来源于stack exchange,提问作者Haus Kit
相关产品推荐
相关产品推荐

