0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GASでXMLをpayloadしてみた

Posted at

経緯

GAS(Google Apps Script)でXMLをパラメーターに設定してあるAPIを実行したかったので、備忘録として記載します。

仕様

XMLに引数を設定します。
下記関数はBasic認証を例にしています。

実装

main.gs
function execApi(arg) {

  var url = 'API_URL';
  
  // ヘッダーの準備(Basic認証)
  var headers = {
    'Authorization' : 'Basic ' + Utilities.base64Encode('user' + ':' + 'password'),
  };
  
  // payload(xml形式)
  // 引数をxmlに埋め込む
  var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><element1>\t<element2>\t\t<element3>" + arg + "</element3>\t</element2></element3>";
  
  // PUTを利用
  var options = {
    'method' : 'put',
    'contentType' : 'application/xml',
    'headers' : headers,
    'payload' : xml
  };
  
  try {
    UrlFetchApp.fetch(url, options);
  } catch(e) {
    Logger.log("エラーが発生しました。\n" + e);
  }
  
}

以上です。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?