Google Apps Script/URLFetchApp and using returned data

旧城冷巷雨未停 提交于 2021-01-29 20:11:36

问题


I am very new to this, so please bear with me-- I have currently have an operational google apps script on the backend of a google sheet that is generated from Google Form answers. I am essentially setting up a ticket form in google forms that will trigger the data in the corresponding sheet to be sent via api call to our ticketing system. It works great, but I am trying to optimize it currently. The goal is to take the json response I get using:

Logger.log(response.getContentText());

which provides me the following info:

Aug 9, 2020, 11:44:40 AM Info {"_url":"https://testticketingsystem.com/REST/2.0/ticket/123456","type":"ticket","id":"123456"}

and send another API call to send data to that new ticket.

Here's a code snippet:

var payload = { 
    "Subject":  String(su),
    "Content":  String(as),
    "Requestor": String(em),
    "Queue": String(qu),
    "CustomFields": {"CustomField1": String(vn), "CustomField2": String(vb), "CustomField3": 
     String(vg), "CustomField4": String(av), "CustomField5": String(ov), "CustomField6": 
     String(sd)}
     }

var options = {
      'method': 'post',
      "contentType" : "application/json",
      'payload': JSON.stringify(payload),
      'muteHttpExceptions': true
       }

var url = "https://testticketingsystem.com/REST/2.0/ticket?token=****************";

var response = UrlFetchApp.fetch(url,options);

Logger.log(response.getContentText());
  
  } catch (error) {
    Logger.log(error.toString());
  }
  }

After the ticket is created, how do I script the use of that ID number as a variable into my next api call?

Thank you!


回答1:


UrlFetchApp.fetch returns a HTTPResponse, and if you expect JSON then you should be able to just use JSON.parse() to create an object from the text. (The JSON object is a standard JavaScript global object like Math; it is not Google Apps Script specific.)

If all goes well, you should just be able to use

var response = UrlFetchApp.fetch(url,options);
var data = JSON.parse(response.getContentText());
var id = data.id;

and then use that id for your next fetch().

Notes

If your literal response is indeed

Aug 9, 2020, 11:44:40 AM Info    {"_url":"https://testticketingsystem.com/REST/2.0/ticket/123456","type":"ticket","id":"123456"}

you will run into trouble as everything until the { is invalid JSON (use a linter if you need to check yourself). But I'm assuming that was added by the console when you logged JSON, and not in the actual response itself.

JSON.parse() throws an error with invalid JSON, so you can use try/catch if needed.

You can also check the headers before you try to JSON.parse().

Here's an example that checks and handles issues, should they arise.

var type = response.getHeaders()["Content-Type"];
var text = response.getContentText();
if (type === "application/json") {
  try {
    var data = JSON.parse(text);
  } catch (error) {
    return Logger.log("Invalid JSON: " + response.getContentText(text));
  }
} else {
  return Logger.log("expected JSON, but got response of type: " + type);
}
// if we get to this line, data is an object we can use


来源:https://stackoverflow.com/questions/63328704/google-apps-script-urlfetchapp-and-using-returned-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!