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

在JavaScript中发送multipart/mixed请求至Google Search Console报错求助

解决Google Search Console批量请求400解析错误

错误原因分析

你的批量请求触发400错误,核心问题在于:

  • 子HTTP请求缺少必需的Host头,Google批量API要求每个子请求必须包含Host字段
  • 使用\n作为行分隔符,不符合HTTP协议要求的\r\n(CRLF)规范
  • 初始空行导致边界解析异常,子请求的HTTP结构不完整

修复后的代码

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.googleapis.com/batch/webmasters/v3');
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.setRequestHeader('Content-Type', 'multipart/mixed; boundary=batch_foobarbaz');

// 用CRLF符合HTTP协议行结束规范
var crlf = '\r\n';
var data = [];       

// 第一个请求块
data.push('--batch_foobarbaz');
data.push('Content-Type: application/http');
data.push('Content-Transfer-Encoding: binary');
data.push(crlf);
data.push('GET /webmasters/v3/sites/' + siteAdress + ' HTTP/1.1');
data.push('Host: www.googleapis.com');
data.push(crlf);

// 第二个请求块
data.push('--batch_foobarbaz');           
data.push('Content-Type: application/http');
data.push('Content-Transfer-Encoding: binary');
data.push(crlf);
data.push('GET /webmasters/v3/sites/' + siteAdress2 + ' HTTP/1.1');
data.push('Host: www.googleapis.com');
data.push(crlf);

// 结束边界
data.push('--batch_foobarbaz--');

var payload = data.join(crlf);        
xhr.send(payload);

关键修复点说明

  1. 替换行分隔符:用\r\n替代\n,严格遵循HTTP协议的行结束要求
  2. 添加必需头信息:每个子请求明确指定HTTP/1.1版本和Host: www.googleapis.com头
  3. 规范请求结构:移除初始空行,子请求头与空体之间用空行分隔,和官方示例结构对齐
  4. 补充编码头:添加Content-Transfer-Encoding: binary,避免解析歧义

修复后请求内容示例

--batch_foobarbaz
Content-Type: application/http
Content-Transfer-Encoding: binary

GET /webmasters/v3/sites/https%3A%2F%2Fdomain.pl%2F HTTP/1.1
Host: www.googleapis.com

--batch_foobarbaz
Content-Type: application/http
Content-Transfer-Encoding: binary

GET /webmasters/v3/sites/https%3A%2F%2Fdomain.pl%2F HTTP/1.1
Host: www.googleapis.com

--batch_foobarbaz--

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 11:40:24