LoginSignup
1
1

More than 3 years have passed since last update.

【GAS】UrlFetchApp fetchの文字コード取得

Last updated at Posted at 2019-09-25

文字コードはヘッダーの中にあるので多分これで取れる。

サンプル


function fechHtmlByUrl(url) {
  const response = UrlFetchApp.fetch(url);
  const headers = response.getHeaders();
  const contentType = headers['Content-Type'].replace(/\s+/g, '');
  const indexOfCharset = contentType.indexOf('charset=');
  const code = (indexOfCharset == -1) ? '' : contentType.substring(indexOfCharset + 8);
  return response.getContentText(code);
}

追記

https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)
公式サイトによればUrlFetchApp.fetchのAdvanced parametersでheadersをtureとすればa JavaScript key/value map of HTTP headersが手に入るらしいがよくわからないのでやってない。

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