如何在angular 7中创建对gmail api的批处理请求

如何在angular 7中创建对gmail api的批处理请求,angular,http,gmail,content-type,batch-request,Angular,Http,Gmail,Content Type,Batch Request,简短而甜蜜的是…在angular 7中,我可以使用什么代码向gmail api批处理端点发出批处理请求 我已经能够使用postman成功地使用gmail api发出批处理请求…使用原始正文格式,但似乎无法从angular 7应用程序中为gmail api批处理端点创建正确的post请求。由于语法无效,我收到错误响应400 在postman中,只需使用授权设置令牌即可:承载和内容类型:multipart/mixed;boundary=“foo\u bar”然后使用以下命令将主体设置为原始请求: -

简短而甜蜜的是…在angular 7中,我可以使用什么代码向gmail api批处理端点发出批处理请求

我已经能够使用postman成功地使用gmail api发出批处理请求…使用原始正文格式,但似乎无法从angular 7应用程序中为gmail api批处理端点创建正确的post请求。由于语法无效,我收到错误响应400

在postman中,只需使用授权设置令牌即可:承载和内容类型:multipart/mixed;boundary=“foo\u bar”然后使用以下命令将主体设置为原始请求:

--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/threads/16805106cf1751bc

--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/threads/16804cfeaeb94c4a

--foo_bar--
我在angular 7中尝试过这一点:

private readonly BATCH_API_URL: string = 'https://www.googleapis.com/batch/gmail/v1';

batchTest() {
  let authToken = this.authService.getToken();
  let body = `--foo_bar
              Content-Type: application/http
              GET /gmail/v1/users/me/threads/16805106cf1751bc

              --foo_bar
              Content-Type: application/http
              GET/gmail/v1/users/me/threads/16804cfeaeb94c4a

              --foo_bar--`

  this.httpClient.post(this.BATCH_API_URL, body, {
    headers: new HttpHeaders({
      'Authorization': `Bearer ${authToken}`,
      'Content-Type': `multipart/mixed; boundary="foo_bar"`
    })
  }).subscribe(response => {
    console.log(response);
  })

}
我也尝试过:

let body = String.raw`--foo_bar\r\nContent-Type: application/http\r\n\r\nGET /gmail/v1/users/me/threads/16805106cf1751bc\r\n\r\n--foo_bar--`
…还有一些其他的长镜头,比如将字符串转换为BufferArray并将其作为主体传入

我正试图找出如何正确地将post请求的主体组成gmail api批处理端点,这样我就可以发出和接收批处理请求……非常感谢能够帮助我解决这个问题的任何人

let body ='--foo_bar\n' +
   'Content-Type: application/http\n\n' + 
   'GET https://www.googleapis.com/gmail/v1/users/me/threads/16805106cf1751bc\n' + 
   '--foo_bar\n' + 
   'Content-Type: application/http\n\n' + 
   'GET https://www.googleapis.com/gmail/v1/users/me/threads/16805106cf1751bc\n' + 
   '--foo_bar--'
和你有类似的问题。通过连接正文的一组字符串而不是使用多行字符串(`)修复了此问题。这似乎增加了不必要的空格和换行符


也可以在批正文中包含完整的HTTP请求。

我真的很抱歉。我注意到我误解了你的处境。在我的环境中,我还确认了没有
内容ID的请求主体可以工作。因为我的答案没有用,我想删除它。顺便说一下,在脚本中,
内容类型:application/http
GET/gmail/v1/users/me/threads/16805106cf1751bc
之间没有空行。这与您的上述请求正文不同。如果使用此选项,则会发生错误。你能再确认一下吗?