如何使用ContentService和doPost来创建REST API [英] How to use ContentService and doPost to create a REST API

查看:105
本文介绍了如何使用ContentService和doPost来创建REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让myFunction()发送一个有效载荷,它将由doPost处理并根据有效载荷返回一些内容。我只能得到这些测试脚本在这些实例中的任何一个中工作:
$ b $ a)使用doGet(),不传递有效载荷但得到按预期返回的输出。 / b>

或b
$ b b)使用doPost(),传递有效载荷,但得到405代码而不是输出。



我缺少什么?

 函数doPost(e){ 
var ss = SpreadsheetApp.openById('<记录参数的电子表格编号>');
var sheet = ss.getSheets()[0];
var record;
for(var i in e.parameters){
record ='parameter:'+ i +'='+ e.parameters [i];
sheet.getRange(sheet.getLastRow()+ 1,1,1,1).setValue(record);
}
var output = ContentService.createTextOutput();
output.setContent(content to return);
返回输出;
}

函数myFunction(){
var url ='<以上脚本webapp的url,设置为匿名,任何人>';
var payload = {payloadToSend:'string to send'};
var method ='post'
var response = UrlFetchApp.fetch(url,{method:method,payload:payload})。getContentText();
Logger.log(response);
return;


解决方案

问题似乎在于域名script.googleusercontent.com(ContentService内容所在的域名)不允许使用POST方法。当您的doPost()处理程序返回TextOuput时,它会将重定向发送回客户端,这将导致客户端再次将POST发送到script.googleusercontent.com,这是不允许的。



如果您可以使用GET,那么这将是推荐的解决方案。如果不是,您可能需要将工作分为两部分:第一次发布时没有响应,然后获取响应。



更新 :截至2012年9月21日,这个问题现在应该得到解决,您可以无误地发布到URL。


I want to have the myFunction() send a payload which will be processed by the doPost and return some content based on the payload. I can only get these test scripts to work in either of these instances:

a) Use doGet(), not pass the payload but get the output returned as expected.

OR

b) Use doPost(), pass the payload but get an 405 code instead of the output.

What am I missing?

function doPost(e) {
  var ss = SpreadsheetApp.openById('<spreadsheet id for recording parameters>');
  var sheet = ss.getSheets()[0];
  var record;
  for (var i in e.parameters) {
    record = 'parameter: ' + i + ' = ' + e.parameters[i];
    sheet.getRange(sheet.getLastRow() + 1, 1, 1, 1).setValue(record);
  }
  var output = ContentService.createTextOutput();
  output.setContent("content to return");
  return output;
}

function myFunction() {
  var url = '<url of above script webapp, set to anonymous, anyone>';
  var payload = {payloadToSend : 'string to send'};
  var method = 'post'
  var response = UrlFetchApp.fetch(url, {method : method, payload: payload}).getContentText();
  Logger.log(response);
  return;
}

解决方案

The problem appears to be that the domain "script.googleusercontent.com", which is where ContentService content is served from, doesn't allow the POST method. When your doPost() handler returns the TextOuput it sends a redirect back to the client, which causes the client to send the POST again to script.googleusercontent.com, which is not allowed.

If you can use GET then that would be the recommended solution. If not, you may need to split the work into two parts: first POSTing with no response, then GETting to retrieve the response.

Update: As of Sept 21, 2012 this problem should now be fixed, and you can POST to the URL without any errors.

这篇关于如何使用ContentService和doPost来创建REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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