Converting @filename in curl to google apps script

Viewed 67

Curl request in the documentation:

  curl -X POST -d "xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls" https://kc.kobotoolbox.org/api/v1/forms

My apps script

function patchForm() {
     //Other details saved in scriptProperties
     var payload = "https://docs.google.com/spreadsheets/d/e/.../pub? 
                   output=xlsx";
     //Published spreadsheet, I think this is essentially an xlsx file stored in a url?
     var scriptProperties = PropertiesService.getScriptProperties();
     var url = "https://kc.kobotoolbox.org/api/v1/forms/...";
     var params = {
       "method" : scriptProperties.getProperties()["method"],
       "headers": {"Authorization" : scriptProperties.getProperties()["Authorization"]},
       "xls_url": payload
        };

     Logger.log(params)
     UrlFetchApp.fetch(url, headers = params)
     return(UrlFetchApp.fetch(url, headers = params));

}

How do I convert the:

     "xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"

In the api documentation inside urlfetchapp.

Thank you, not really a programmer.

If I cant get urlfetchapp to work, is there a way to send a curl request via google script?

1 Answers

I believe your goal as follows.

  • You want to convert the following curl command to Google Apps Script.

      curl -X POST -d "xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls" https://kc.kobotoolbox.org/api/v1/forms
    

Modification points:

  • The property of xls_url is not existing in params of rlFetchApp.fetch(url, params).
  • When I saw your sample curl command, the value of xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls is sent as the form data.
  • In this case, the method is the POST method.
  • When I saw your script and your sample curl command, the URL and value are different.

When above points are reflected to the Google Apps Script, it becomes as follows.

Modified script:

function patchForm() {
  var url = "https://kc.kobotoolbox.org/api/v1/forms";
  var params = {
    method: "POST", // In this case, even when this line is removed, the POST method is used.
    payload: {xls_url: "https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"}
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText())
}

Note:

  • When you want to use https://docs.google.com/spreadsheets/d/e/.../pub?output=xlsx instead of https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls, please replace it.

  • In your curl command, the request header is not used. But in your script, "Authorization" : scriptProperties.getProperties()["Authorization"] is used. If this is required to be used, please include it to above script as follows. In this case, it supposes that your value of scriptProperties.getProperties()["Authorization"] is valid value. Please be careful this.

      var scriptProperties = PropertiesService.getScriptProperties();
      var params = {
        method: "POST", // In this case, even when this line is removed, the POST method is used.
        payload: {xls_url: "https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"},
        headers: {"Authorization" : scriptProperties.getProperties()["Authorization"]},
      };
    

Reference:

Related