[[:gas|一つ上へ]]
function httpget() {
const url = 'https://jsonplaceholder.typicode.com/posts/1';
const response = UrlFetchApp.fetch(url);
if (response.getResponseCode() === 200) {
const data = JSON.parse(response.getContentText());
Logger.log(data.title);
} else {
Logger.log('GET request failed. Status code: ' + response.getResponseCode());
}
}
===== POSTパターン =====
function httpclient() {
const url = 'url'; // URLを入力
const payload = {
title: 'Sample Post',
body: 'This is a sample post.',
userId: 1
};
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 201) {
const data = JSON.parse(response.getContentText());
Logger.log(data);
} else {
Logger.log('POST request failed. Status code: ' + response.getResponseCode());
}
}