Google Apps脚本:UrlFetchApp发布文件 [英] Google Apps Script: UrlFetchApp Post File

查看:216
本文介绍了Google Apps脚本:UrlFetchApp发布文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Google Apps脚本将文件发布到REST API。我的想法是,我有一个创建Go​​ogle文档副本的过程,我希望能够将这些新创建的文档发布到第三方系统。

我在 UrlFetchApp 中发现我可以发送文件。不过,我在发送正确的标题值时遇到了问题。



我的请求如下所示:

  var file = DriveApp。 getFileById(FILEID); 
var body = {
file:file.getAs(MimeType.PDF)
};
var headers = {
'Content-Disposition':'attachment; filename ='+ file.getName()+'',
'Content-Length':file.getSize()
};

当我调用UrlFetchApp.fetch(url,options)时,我的选项如下所示:

 ({
method:POST,
headers:{
'Content-Disposition':attachment ; filename = \My Merge Development_row_1.pdf \,
'Content-Length':90665,
授权:Bearer TOKEN
},
contentType: application / x-www-form-urlencoded,
muteHttpExceptions:true,
payload:{file:Blob}
})

我发送文件的API需要'Content-Length'标题。但是,当我尝试为'Content-Length'标头设置一个值时,我得到一个Apps脚本错误,属性提供了无效值:Header :Content-Length的。如果我没有设置 Content-Length 头部,API会响应 Content-Length 和文件大小不匹配。

有关如何设置 Content-Length 标题的任何想法,以便我可以发布文件?

解决方案

现有票据强调文档在这个问题上并不明确



解决方案是:
$ b


将内容长度值从
头中的content-Length名称/值对移动到高级参数 contentLength


因此,在您的示例中,您的选项应该看起来像

 ({
method:POST,
headers:{
'Content-Disposition':attachment; filename = \My Merge Development_row_1.pdf \,
授权:Bearer TOKEN
},
contentLength:90665,
contentType:application / x-www-form-urlencoded,
muteHttpExceptions:true,
payload:{file:Blob}
})

编辑:添加了一个完整的示例函数来获取con tentLength和blob如下所示:

  function testFilePost(){
var file = DriveApp.getFileById(doc_id).getAs (MimeType.PDF);
var headers = {
'Content-Disposition':'attachment; filename ='+ file.getName()+'',
};
var options =
{
method:post,
payload:file.getBytes(),
headers:headers,
contentLength:file.getBytes()。length,
};
var result = JSON.parse(UrlFetchApp.fetch('http://httpbin.org/post',options).getContentText());
Logger.log(result);
}


I'm trying to POST a file to a REST API via Google Apps Script. The idea is that I have a process that is creating copies of a Google Doc, and I want to be able to post those newly created Docs to a third party system.

I found in UrlFetchApp that I can send files. However, I'm having issues sending the correct header values.

My request looks like so:

var file = DriveApp.getFileById(fileId);
var body = {
  "file": file.getAs(MimeType.PDF)
};
var headers = {
  'Content-Disposition': 'attachment; filename="'+ file.getName() +'"',
  'Content-Length': file.getSize()
};

My options when I call UrlFetchApp.fetch(url, options) looks like :

({
  method:"POST", 
  headers:{
      'Content-Disposition':"attachment; filename=\"My Merge Development_row_1.pdf\"", 
      'Content-Length':90665, 
       Authorization:"Bearer TOKEN"
  }, 
  contentType:"application/x-www-form-urlencoded", 
  muteHttpExceptions:true, 
  payload:{file:Blob}
})

The API that I'm sending the files to requires the 'Content-Length' header. But, when I try to set a value for 'Content-Length' header I get an Apps Script error, "Attribute provided with invalid value: Header:Content-Length". If I don't set the Content-Length header then the API responds that the Content-Length and file size don't match.

Any ideas on how I set the Content-Length header so I can POST the file?

解决方案

There is an existing ticket highlighting that the documentation is not clear on this very issue

The solution is:

Move content length value from "content-Length" name/value pair in headers to the advanced argument "contentLength"

So in your example your options should look like

({
  method:"POST", 
  headers:{
      'Content-Disposition':"attachment; filename=\"My Merge Development_row_1.pdf\"", 
       Authorization:"Bearer TOKEN"
  }, 
  contentLength: 90665,
  contentType:"application/x-www-form-urlencoded", 
  muteHttpExceptions:true, 
  payload:{file:Blob}
})

EDIT: Added a full example function to get contentLength and blob shown below:

function testFilePost() {
  var file = DriveApp.getFileById(doc_id).getAs(MimeType.PDF);
  var headers = {
    'Content-Disposition': 'attachment; filename="'+ file.getName() +'"',
  };
  var options =
      {
        "method" : "post",
        "payload": file.getBytes(),
        "headers": headers,
        "contentLength": file.getBytes().length,
      };
  var result = JSON.parse(UrlFetchApp.fetch('http://httpbin.org/post', options).getContentText());
  Logger.log(result);
}

这篇关于Google Apps脚本:UrlFetchApp发布文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆