LoginSignup
1
0

More than 1 year has passed since last update.

GAS(Google Apps Script)のUrlFetchAppのリクエストbodyに配列をセットする

Last updated at Posted at 2022-03-15

失敗するパターン

オブジェクトの配列をそのままpayloadにセット

  const reqOptions = {
      method: 'post',
      payload: [{sample: 1}, {sample: 2}]
    }
  const res = UrlFetchApp.fetch(Config.API.URL, reqOptions);

単純なJSON.stringify

  const reqOptions = {
      method: 'post',
      payload: JSON.stringify([{sample: 1} {sample: 2}])
    }
  const res = UrlFetchApp.fetch('https://xxx', reqOptions);

成功するパターン

contentType: 'application/json' をセットする

  const reqOptions = {
      method: 'post',
      contentType: 'application/json', // これ!!!
      payload: JSON.stringify([{sample: 1} {sample: 2}])
    }
  const res = UrlFetchApp.fetch('https://xxx', reqOptions);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0