e.postData in doPost(e) function of Google Apps Script

Google Apps Script(GAS)의 doPost(e) 함수에서 postData는 HTTP POST 요청으로 전달된 데이터를 포함하고 있는 객체의 일부입니다. doPost(e)에서 전달되는 매개변수 e는 여러 정보를 담고 있는 객체이고, 그 안에 postData라는 속성이 있습니다.

doPost(e)의 구조 요약

function doPost(e) {
  Logger.log(e.postData);
}

e.postData 객체의 주요 속성:

속성 설명
contents POST 요청의 본문 내용 (string 형태)
length 본문 길이 (문자 수)
type Content-Type 헤더 (application/json, application/x-www-form-urlencoded, 등)
name 파일 업로드와 관련된 이름 (자주 사용되지 않음)

예제 1: JSON 데이터를 받는 경우

클라이언트에서 JSON으로 데이터를 보냈다면:

// 예: 클라이언트가 이런 요청을 보냈다고 가정
// fetch(url, {
//   method: 'POST',
//   headers: { 'Content-Type': 'application/json' },
//   body: JSON.stringify({ name: "Alice", age: 25 })
// });

function doPost(e) {
  const json = JSON.parse(e.postData.contents);
  Logger.log(json.name); // "Alice"
}

예제 2: 폼 형식 데이터 (application/x-www-form-urlencoded)

// 예: fetch with form data
// fetch(url, {
//   method: 'POST',
//   headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
//   body: "name=Alice&age=25"
// });

function doPost(e) {
  Logger.log(e.parameter.name); // "Alice"
  Logger.log(e.parameter.age);  // "25"
}

즉, postData.contents는 POST 본문 전체 문자열, parameter는 GAS가 자동으로 파싱한 쿼리 파라미터/폼 데이터입니다.

참고

https://breezegroup.co.jp/201906/gas-get/