如何在 NodeJs 中发送 multipart/mixed 请求 google 索引批量请求?

How to send multipart/mixed request for google indexing batch request in NodeJs?

我正在使用 Nodejs 连接 GoogleApis v35.0.0 to tell Google to update or remove pages from the Google index. And I stuck in the multipart/mixed request, the body of multipart when I send the request through Google indexing batch request

我可以通过 indexing API documentation 向 Google 发送单个页面更新请求。但是由于 Google 的配额有限,每天最多 200 个请求,因此我需要更新比这更多的 URL。因此,我正在尝试使用 google 索引批处理请求,它最多可以对 100 个单独的请求进行分组,并且计为 1 个请求。

当我尝试批量发送请求时,我遇到了 multipart body 的正确格式问题。我正在使用从 oauth2 扩展而来的 GoogleApis 的 JWT(JSON Web 令牌)来验证我的帐户,并使用 request library v2.88.0 将请求发送到 Google。

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

根据 Google 中的批量请求 body 示例,我需要在主请求中使用 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" }

这是我的代码:

    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);
      });

    });

我在 multipart 的 body 中遇到错误,我不知道正在等待哪种 body google 索引批量请求。似乎 multipart body 中的所有内容都被视为 headers。但是根据文档批量请求的格式,它说 "Each part begins with its own Content-Type: application/http HTTP header. The body of each part is itself a complete HTTP request, with its own verb, URL, headers, and body"。有关更多详细信息,请查看: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"
  }
}

有人知道 body 请求 google 索引批量请求时,multipart 中 body 的正确格式是什么吗?

提前致谢!

批处理无助于避免配额限制

I could able to send an individual page update request to Google by following the indexing API documentation. But since Google has the limited quota at maximum of 200 requests per day and I need to update more URL's than that. So, I am trying to use google indexing batch request which can group at maximum of 100 individual requests and it counts as 1 request.

batching 中没有任何内容表明它仅计入您的配额。

虽然批处理可以节省您构建许多 HTTP 请求的开销,但批处理请求中的每个 Google API 请求都将计入您的每日项目配额。默认情况下,一个项目每天最多可以发出 200 个请求;批处理不会帮助您保持低于此配额。

申请更高的配额

您是否考虑过申请更高的配额?我知道回复回复可能需要一些时间,但您可能只想等着看他们怎么说。

注google-apis-nodejs-client

该库不支持批处理,因此您将不得不像现在这样自己做 #1130

您的实际问题

如果您想继续尝试进行批处理,请告诉我。我看看能不能帮忙。带手动版。

正如@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) => {
  //...
});