我有一个冗长的卷曲请求,我想使用UrlFetchApp模拟:
curl 'https://example.com/index.php' -H 'Cookie: "XXXXXXXXXX"' -H 'Origin: https://example.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryK0ckVcd9Rae277Ae' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Referer: https://example.com/index.php' -H 'Connection: keep-alive' --data-binary $'------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="user"\r\n\r\nstaff\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="name"\r\n\r\nJohn Smith\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="staffid"\r\n\r\ne00000\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="preferred"\r\n\r\nEmail\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="phone"\r\n\r\n9999 9999\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="altemail"\r\n\r\njohn.smith@example.com\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="subject"\r\n\r\nother\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="desc"\r\n\r\nTEST ONLY.\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="userfield"\r\n\r\n\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="timezone"\r\n\r\nMon Mar 23 2015 10:28:33 GMT+1100 (AEDT)\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="browserstring"\r\n\r\nMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="files[]"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae--\r\n' --compressed
我正在使用以下UrlFetchApp代码,该代码只创建一个有效负载和一个头对象,并将有效负载编码为二进制blob:
function sendHttpPost(mailBody) {
var reqPayload = '------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="rmituser"\r\n\r\nstaff\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="name"\r\n\r\nJohn Smith\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="staffid"\r\n\r\nE00000\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="phone"\r\n\r\n9999 9999\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="altemail"\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="subject"\r\n\r\nother\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="desc"\r\n\r\nTEST ONLY.\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="userfield"\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="timezone"\r\n\r\nThu Mar 19 2015 16:25:47 GMT+1100 (AEDT)\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="browserstring"\r\n\r\nMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="files[]"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9--\r\n';
var reqHeaders = {
'Cookie' : 'XXXXXXXXXXXXXXX',
'Origin' : 'https://example.com',
'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : 'en-US,en;q=0.8',
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
'Content-Type' : 'multipart/form-data; boundary=----WebKitFormBoundaryK0ckVcd9Rae277Ae',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control' : 'max-age=0',
'Referer' : 'https://example.com/index.php',
'Connection' : 'keep-alive'
};
var blob = Utilities.newBlob(reqPayload);
var options =
{
'method' : 'post',
'payload' : blob.getBytes(),
'headers' : reqHeaders,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch("https://www.example.com/index.php", options);
Logger.log("Response Full: " + response);
}
虽然我已经修改了一些细节以匿名化示例(包括删除cookie详细信息),但这两个请求应该是相同的。 curl请求有效,UrlFetchApp请求是根据curl请求的复制构建的。但是UrlFetchApp请求返回404。
我很想知道我是否错过了任何明显的事情。
答案 0 :(得分:0)
您正在定义' reqHeaders'但使用' reqHeadersMin'。
你说它是PUT但是我没有在curl命令中看到-X PUT,所以我认为它默认为GET。并且您将fetch()调用定义为选项中的POST。
答案 1 :(得分:0)
原来这里的问题与防火墙有关。
在不起作用的情况下,UrlFetchApp目标位于防火墙后面。 curl命令用于从防火墙内的计算机上的终端发送的命令。但是,Google的Apps脚本服务器位于该防火墙之外,因此UrlFetchApp请求失败。