如何在Google Apps脚本中获取位置标题 [英] How to get the location header in Google Apps Script
问题描述
我正在尝试构建一个Google Spreadsheet,其中:
我在填充列D时遇到了问题。以下是我的尝试:
函数getHeaders(tURL){
var response = UrlFetchApp.fetch(tURL);
var tHeaders = response.getAllHeaders();
}
我期待这个位置会出现在响应头中。但似乎这种方法只返回少数服务器响应头。我得到:X-Frame-Options,Date,P3P,Content-Length,Expires,X-XSS-Protection,Content-Encoding,Alternate-Protocol,Set-Cookie(array),Content-Type,Server,Cache-Control。
问题
有关另一种方式到达位置的建议吗?或者有关如何确定url被重定向到哪里的替代想法?
followRedirects
参数设置为 false
: var response = UrlFetchApp.fetch(tURL,{'followRedirects':false});
followRedirects
默认为 true
,因此您的请求会返回它被重定向到的页面的标题和内容,而不是请求的页面。这就是为什么你没有看到位置标题。
I was experimenting with building a Google Spreadsheet where:
- Column A is a list of URLs that should be 301 redirected
- Column B was our intended URL to be redirect to
- Column C is the server response code when hitting Column A
- Column D is the URL that was ultimately served up when hitting Column A
- Column E is a Boolean for B and D matching
I'm having problems with populating column D. Here is what I tried:
function getHeaders(tURL) {
var response = UrlFetchApp.fetch(tURL);
var tHeaders = response.getAllHeaders();
}
I was expecting that "location" would be in the response header. But it seems this method only returns a handful of server response headers. I get: X-Frame-Options, Date, P3P, Content-Length, Expires, X-XSS-Protection, Content-Encoding, Alternate-Protocol, Set-Cookie (array), Content-Type, Server, Cache-Control.
Question Any suggestions on another way to get to location? Or an alternate idea on how to determine where the url was redirected to?
You need to use fetch(url, params) method of UrlFetchApp class and supply followRedirects
parameter set to false
:
var response = UrlFetchApp.fetch(tURL, {'followRedirects':false});
followRedirects
defaults to true
, and thus your request returns the headers and content of the page it was redirected to, not the requested page. That is why you do not see the Location header there.
这篇关于如何在Google Apps脚本中获取位置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!