Google索引批处理请求发送

Google索引批处理请求发送

本文介绍了如何在NodeJs中为Google索引批处理请求发送多部分/混合请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nodejs与 GoogleApis v35.0.0 连接,以告诉Google更新或从Google索引中删除页面.当我通过 Google索引批处理请求发送请求时,我陷入了multipart/mixed请求中,即多部分正文 a>.

I am using Nodejs to connect with 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.

通过遵循索引API文档.但是由于Google的配额有限,每天最多只能有200个请求,因此我需要更新的URL数量要更多.因此,我尝试使用Google索引批处理请求,该请求最多可以将100个单独的请求分组,并且算作1个请求.

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.

当我尝试分批发送请求时,多主体的正确格式出现问题.我正在使用从oauth2扩展来验证我的帐户的GoogleApis的JWT(JSON Web令牌),并使用了请求库v2. 88.0 将请求发送给Google.

I am having issue with the correct format of multipart body when I am trying to send the request by batch. I am using JWT (JSON Web Token) of GoogleApis which extended from oauth2 to authenticate my account and using the request library v2.88.0 to send the request to Google.

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

Since request library already handle the multipart boundary that's why I am not sending that as one of the request options information. I also check the information in the multipart/mixed of the request npm library but I only found the one similar but not the same which is the multipart/related (https://github.com/request/request#multipartrelated).

根据来自 Google ,我需要在主要请求中使用multipart/mixed作为内容类型:

According to the piece of batch request body example from Google, I need to use multipart/mixed as content type in the main request:

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的正文中遇到错误,我不知道google indexing批处理请求正在等待哪种正文.似乎multipart主体中的所有内容都被视为标头.但是根据文档中批处理请求的格式,它说:每个部分都以其自己的Content-Type开头:application/http HTTP标头.每个部分的主体本身就是一个完整的HTTP请求,带有其自身的动词,URL,标头和正文".有关更多详细信息,请检查: https://cloud.google. com/storage/docs/json_api/v1/how-tos/batch .

I am getting error in the body of multipart, I don't know which kind of body google indexing batch request is waiting for. Seems like everything inside the body of multipart are considering as headers. But according to the documentation the format of batch request, it says that "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". For more details information check: https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch.

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

However, I am getting the following error when I execute my code:

{
  "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"
  }
}

有人请求Google索引批处理请求时,有人知道多部分体内的正确格式是什么吗?

Does someone know which is the correct format of body inside the multipart when it request to google indexing batch request?

谢谢!

推荐答案

@DalmTo表示配额仍然适用,即使是批处理请求也是如此.但是您也没有正确构造有效负载,下面的示例可以正常工作.

As @DalmTo says the quota will still apply, even to batch requests. But also you are not correctly constructing the payload, the following example works.

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

这篇关于如何在NodeJs中为Google索引批处理请求发送多部分/混合请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:20