Code Coca

How to create post request in Google App Script

To create a POST request in Google Apps Script, you can use the built-in UrlFetchApp service. Here's an example of how to create a basic POST request:

App Script
function createPostRequest() {
var url = "https://example.com/api"; // Replace with your API endpoint
var payload = {
key1: "value1",
key2: "value2"
};
var options = {
method: "POST",
payload: payload,
contentType: "application/json"
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}

In this example, we're using the UrlFetchApp service to make a POST request to the specified API endpoint. The payload variable contains the data we want to send with the request, which is a simple JSON object with two key-value pairs.

The options variable specifies the HTTP method (POST), the payload data, and the content type (application/json).

Finally, we call the fetch() method on the UrlFetchApp object, passing in the URL and options. The response object is returned, which we can then log to the console using Logger.log().