为什么我不能让UrlFetchApp.fetch使用doGet()? [英] Why can't I get my UrlFetchApp.fetch to use the doGet()?

查看:67
本文介绍了为什么我不能让UrlFetchApp.fetch使用doGet()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用两种相同的形式,但一种形式使用GET方法,一种形式使用POST方法,当我运行它们时,一种形式使用doGet而另一种形式使用doPost().但是,当我通过提供Prompt的输入来对UrlFetch()命令执行相同的操作时,不会发生这种情况.

If I use two forms that are both the same but one has a method of GET and one has a method of POST when I run them one uses the doGet and the other uses the doPost(). But that doesn't happen when I do the same thing with the UrlFetch() command by providing the input from a Prompt.

html:

最上面的形式是GET,最下面的形式是POST.

The top form is a GET and the bottom form is POST.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <h3>GET</h3>
    <form action="https://script.google.com/a/cooper/macros/s/AKfycbzlYgw2cw8q7u9qTxM/exec" enctype="application/x-www-form-urlencoded" method="GET">
      <input type="text" name="one" value="" />
      <br /><input type="text" name="two" value="" />
      <br /><textarea rows="2" cols="25" name="three" placeholder="Enter Text Here"></textarea>
      <br /><input type="submit" value="Submit" />
    </form>
 <h3>POST</h3>   
    <form action="https://script.google.com/a/cooper/macros/s/AKfycbzlYgw2cw8q7u9qTxM/exec" enctype="application/x-www-form-urlencoded" method="POST">
      <input type="text" name="one" value="" />
      <br /><input type="text" name="two" value="" />
      <br /><textarea rows="2" cols="25" name="three" placeholder="Enter Text Here"></textarea>
      <br /><input type="submit" value="Submit" />
    </form>
  </body>
</html>

Code.gs:

function doGet(e){
  Logger.log('doGet: %s',JSON.stringify(e));
  if(e.parameter) {
    var data=e.parameter;
    handleFunction(data,'get');//This hardwires the term get to the ouput
  }
  return HtmlService.createHtmlOutputFromFile('testing');
}

function doPost(e){
  console.log('doPost Entry',JSON.stringify(e));
  if(e.postData.length!=-1) {
    var data=e.parameter;
    handleFunction(data,'post');//This hardwires the term post to the ouput
  }
  return ContentService.createTextOutput('doPost Return:' + JSON.stringify(e.postData.contents));
}

function handleFunction(data,source){
  console.log('handleFunction Entry: %s',JSON.stringify(data));
  var one=data.one;
  var two=data.two;
  var three=data.three;
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getActiveSheet();
  sheet.appendRow([source,one,two,three]);
}

function postit() {
  // DriveApp.getFiles() //I copied this from a Tanaike example.  I don't know if I need it still
  var ask=SpreadsheetApp.getUi().prompt("Enter first/second/third/method", SpreadsheetApp.getUi().ButtonSet.OK) 
  if(ask.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
    var dA=ask.getResponseText().split('/');//Just an easy way to get 4 pieces of data at one time
    if(dA.length==4) {
      var url=ScriptApp.getService().getUrl();
      var data={"one":dA[0],"two":dA[1],"three":dA[2]};
      //the method is provide from the last term of the above prompt
      var options={"method":dA[3],"payload":data,"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
      var resp=UrlFetchApp.fetch(url, options);
      Logger.log('UrlFetch Response: %s',resp);
    }
  }
}

动画:

这是我最终得到的结果,它可以在Rhino和V8中运行

function postit() {
  // DriveApp.getFiles() 
  var ask=SpreadsheetApp.getUi().prompt("Enter first/second/third/method", SpreadsheetApp.getUi().ButtonSet.OK); 
  if(ask.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
    var dA=ask.getResponseText().split('/');
    if(dA.length==4) {  
      if(dA[3]=="POST") {
        var url=ScriptApp.getService().getUrl();
        var data={"one":dA[0],"two":dA[1],"three":dA[2]};
        var options={"method":dA[3],"payload":data,"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
      }else if(dA[3]=="GET") {
        var url=Utilities.formatString('%s?one=%s&two=%s&three=%s',ScriptApp.getService().getUrl(),dA[0],dA[1],dA[2]);
        var data={"one":dA[0],"two":dA[1],"three":dA[2]};
        var options={"method":dA[3],"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
      }
      var resp=UrlFetchApp.fetch(url, options);
    }
  }
}

推荐答案

这个答案怎么样?请认为这只是几个可能的答案之一.

How about this answer? Please think of this as just one of several possible answers.

我认为您出现问题的原因是由于 POST GET 方法使用 payload 请求了数据.在UrlFetchApp中,似乎将数据用作<有效负载>时,即使<有效方法> 都使用POST方法.为了将数据作为GET方法发送,请将其添加到查询参数中.

I think that the reason of your issue is due to the data is requested with payload for POST and GET methods. At UrlFetchApp, it seems that when the data is used as payload, POST method is used, even when method is GET. In order to send the data as GET method, please add them to the query parameter.

(我以为使用GET方法将数据作为有效载荷发送时,以前可以将数据作为GET发送.但是关于这一点,我只有一个记忆力较弱.对此我深表歉意.

(I thought that when the data is send as payload with the GET method, the data could be sent as GET before. But about this, I only had a faint memory. I apologize for this.)

为避免这种情况,下面的修改作为简单修改怎么样?

In order to avoid this, how about the following modification as the simple modification?

在此模式中,查询参数用于GET方法.

In this pattern, the query parameter is used for the GET method.

function postit() {
  // DriveApp.getFiles() //I copied this from a Tanaike example.  I don't know if I need it still
  var ask=SpreadsheetApp.getUi().prompt("Enter first/second/third/method", SpreadsheetApp.getUi().ButtonSet.OK) 
  if(ask.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
    var dA=ask.getResponseText().split('/');//Just an easy way to get 4 pieces of data at one time
    if(dA.length==4) {
      var url=ScriptApp.getService().getUrl();

      // --- I modified below script.
      var potions = {};
      if (dA[3] == "POST") {
        var data={"one":dA[0],"two":dA[1],"three":dA[2]};
        //the method is provide from the last term of the above prompt
        options={"method":dA[3],"payload":data,"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
      } else if (dA[3] == "GET") {
        options={"method":dA[3],"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
        url += `?one=${dA[0]}&two=${dA[1]}&three=${dA[2]}`;
      }
      // ---

      var resp=UrlFetchApp.fetch(url, options);
      Logger.log('UrlFetch Response: %s',resp);
    }
  }
}

注意:

  • 请启用V8.
  • 注释行的
  • //DriveApp.getFiles()用于自动添加 https://www.googleapis.com/auth/drive.readonly 使用脚本编辑器.这用于使用访问令牌访问Web Apps.
  • Note:

    • Please enable V8.
    • // DriveApp.getFiles() of the comment line is used for automatically adding the scope of https://www.googleapis.com/auth/drive.readonly with the script editor. This is used for accessing to Web Apps using the access token.
    • 如果我误解了您的问题,而这不是您想要的方向,我深表歉意.

      If I misunderstood your issue and this was not the direction you want, I apologize.

      这篇关于为什么我不能让UrlFetchApp.fetch使用doGet()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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