Skip to content

Instantly share code, notes, and snippets.

@umyuu
Created August 30, 2018 23:55
Show Gist options
  • Save umyuu/83a998fb3d32ce35f751354db39dc7e8 to your computer and use it in GitHub Desktop.
Save umyuu/83a998fb3d32ce35f751354db39dc7e8 to your computer and use it in GitHub Desktop.
GAS(Google Apps Script)でクエリ文字列作成君

GASはURLSearchParams インターフェイスが未実装なため、
QueryStringを作成する関数を定義。

function QueryString(obj, encode) {
  // :param encode use encodeURIComponent defalut:false
  return Object.keys(obj).map(function(key) {
    if (encode) {
      return key + '=' + encodeURIComponent(obj[key]);
    }else {
      return key + '=' + obj[key];
    }
  }).join('&');
}
function myFunction() {
  var params = {
    "Sign": "XXXXXXX",
    "impersonate_email": "[email protected]"
  };
  Logger.log(QueryString(params));
  var Request = {
    "method" : "POST",
    'payload' : QueryString(params)
  }
}

厳密なクエリ文字列を作成したい場合は、以下を参照の事。
◆参考情報
encodeURIComponentが世界基準だと誤解してた話

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment