如何在NodeJs中发送谷歌索引批量请求的多部分/混合请求?

Jin*_*ing 5 multipart google-api node.js google-api-nodejs-client google-indexing-api

我使用Nodejs与GoogleApis v35.0.0连接,告诉Google更新或删除Google索引中的网页.当我通过Google索引批量请求发送请求时,我陷入了multipart/mixed请求,多部分的主体.

我可以通过索引API文档向Google发送单独的页面更新请求.但由于Google每天最多20​​0个请求的配额有限,我需要更新更多的URL.所以,我正在尝试使用谷歌索引批量请求,它可以分组最多100个单独的请求,它计为1个请求.

当我尝试按批次发送请求时,我遇到了正确的多部分正文格式问题.我正在使用GoogleApis的JWT(JSON Web Token),它从oauth2扩展到我的帐户身份验证并使用请求库v2.88.0将请求发送给Google.

由于请求库已经处理了多部分边界,这就是我不将其作为请求选项信息之一发送的原因.我还检查了请求npm库的multipart/mixed中的信息,但我只发现了一个类似但不相同的是multipart/related(https://github.com/request/request#multipartrelated).

根据Google的批量请求正文示例,我需要在主要请求中使用multipart/mixed作为内容类型:

POST /batch HTTP/1.1
Host: indexing.googleapis.com
Content-Length: content_length
Content-Type: multipart/mixed; boundary="===============7330845974216740156=="
Authorization: Bearer oauth2_token

--===============7330845974216740156==
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: <b29c5de2-0db4-490b-b421-6a51b598bd22+2>

POST /v3/urlNotifications:publish [1]
Content-Type: application/json
accept: application/json
content-length: 58

{ "url": "http://example.com/jobs/42", "type": "URL_UPDATED" }
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    return jwtClient.authorize(function(err, tokens) {
      if (err) {
        console.log(err);
        return;
      }

      let options = {
        url: 'https://indexing.googleapis.com/batch',
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/mixed'
        },
        auth: { 'bearer': tokens.access_token },
        multipart: [
          {
            body: JSON.stringify({
              headers: {
                'Content-Type': 'application/http'
              },
              method: 'POST',
              url: 'https://indexing.googleapis.com/v3/urlNotifications:publish',
              body: {
                'Content-Type': 'application/json',
                url: 'https://www.test.com/es/1234',
                type: 'URL_UPDATED'
              }
            })
          }
        ]
      };

      request(options, function (error, response, body) {
        console.log(body);
      });

    });
Run Code Online (Sandbox Code Playgroud)

我在多部分的主体中得到错误,我不知道哪种机构谷歌索引批处理请求正在等待.好像多部分体内的一切都在考虑作为标题.但根据文档批处理请求的格式,它说"每个部分都以自己的Content-Type:application/http HTTP标头开头.每个部分的主体本身就是一个完整的HTTP请求,有自己的动词,URL,标题和正文".有关详细信息,请访问:https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch.

但是,执行代码时出现以下错误:

{
  "error": {
    "code": 400,
    "message": "Failed to parse batch request, error: Failed in parsing HTTP headers: {\"Content-Type\":\"application/http\",\"method\":\"POST\",\"url\":\"https://indexing.googleapis.com/v3/urlNotifications:publish\",\"body\":{\"Content-Type\":\"application/json\",\"url\":\"https://www.test.com/es/1234\",\"type\":\"URL_UPDATED\"}}\n. Received batch body: ",
    "status": "INVALID_ARGUMENT"
  }
}
Run Code Online (Sandbox Code Playgroud)

当请求谷歌索引批量请求时,有人知道多部分内部正确的正文格式吗?

谢谢你!

Dve*_*Dve 4

正如 @DalmTo 所说,配额仍然适用,即使是批量请求。但您也没有正确构建有效负载,以下示例有效。

const items = batch
  .filter(x => x)
  .map(line => {
    return {
      'Content-Type': 'application/http',
      'Content-ID': batchId,
      body:
        'POST /v3/urlNotifications:publish HTTP/1.1\n' +
        'Content-Type: application/json\n\n' +
        JSON.stringify({
          url: line,
          type: 'URL_UPDATED',
        }),
    };
  });
const options = {
  url: 'https://indexing.googleapis.com/batch',
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/mixed',
  },
  auth: { bearer: access_token },
  multipart: items,
};
request(options, (err, resp, body) => {
  //...
});
Run Code Online (Sandbox Code Playgroud)