使用 BridgeTalk HttpConnection object 时如何在 Adobe Illustrator ExtendScript 中设置 http headers?
How do I set http headers in Adobe Illustrator ExtendScript when using the BridgeTalk HttpConnection object?
我正在尝试从 Illustrator ExtendScript(通过 BridgeTalk)中发出 http post 请求,并且大部分情况下它都能正常工作。但是,有关使用 HttpConnection 的文档是 non-existent,我正在尝试弄清楚如何设置 http-headers。 HttpConnection object 有请求headers 和响应headers 属性 所以我怀疑这是可能的。
默认情况下,post 请求与 Content-Type header "text/html" 一起发送,我想覆盖它以便我可以使用 "application/x-www-form-urlencoded" 或 "multipart/form-data".
这是我目前的情况:
var http = function (callback) {
var bt = new BridgeTalk();
bt.target = 'bridge' ;
var s = '';
s += "if ( !ExternalObject.webaccesslib ) {\n";
s += " ExternalObject.webaccesslib = new ExternalObject('lib:webaccesslib');\n";
s += "}\n";
s += "var html = '';\n";
s += "var http = new HttpConnection('http://requestb.in/1mo0r1z1');\n";
s += "http.method = 'POST';\n";
s += "http.requestheaders = 'Content-Type, application/x-www-form-urlencoded'\n";
s += "http.request = 'abc=123&def=456';\n";
s += "var c=0,t='';for(var i in http){t+=(i+':'+http[i]+'***');c++;}t='BEFORE('+c+'):'+t;alert(t);\n"; // Debug: to see what properties and values exist on the http object
s += "http.response = html;\n";
s += "http.execute() ;\n";
s += "http.response;\n";
s += "var t='AFTER:';for(var i in http){t+=(i+':'+http[i]+'***');}alert(t);\n"; // Debug: to see what properties and values have been set after executing
bt.body = s;
bt.onResult = function (evt) {
callback(evt);
};
bt.onError = function (evt) {
callback(evt);
};
bt.send();
};
注意事项:
- 如果我尝试像上面的代码一样设置请求headers 属性,请求将失败。如果我将其注释掉,则请求成功。 requestheaders 的默认值未定义。
- 在请求成功后检查 http object,显示响应headers 属性设置为:"Connection, keep-alive,Content-Length, 2,Content-Type, text/html; charset=utf-8,Date, Wed, 24 Jun 2015 09:45:40 GMT,Server, gunicorn/18.0,Sponsored-By, https://www.runscope.com,Via, 1.1 vegur"。在请求执行之前,responseheaders 被设置为 undefined.
如果有人能帮我设置请求headers(特别是Content-Typeheader),我将不胜感激!
解决了!
设置content-typeheader的关键是设置http.mime属性如下:
s += "http.mime = 'application/x-www-form-urlencoded';\n";
另外,为了完整起见,您可以添加自己的自定义 header,如下所示:
s += "http.requestheaders = ['My-Sample-Header', 'some-value'];\n";
(原来 headers 是一个数组,其格式为 [key1, value1, key2, value2, .......])
我正在尝试从 Illustrator ExtendScript(通过 BridgeTalk)中发出 http post 请求,并且大部分情况下它都能正常工作。但是,有关使用 HttpConnection 的文档是 non-existent,我正在尝试弄清楚如何设置 http-headers。 HttpConnection object 有请求headers 和响应headers 属性 所以我怀疑这是可能的。
默认情况下,post 请求与 Content-Type header "text/html" 一起发送,我想覆盖它以便我可以使用 "application/x-www-form-urlencoded" 或 "multipart/form-data".
这是我目前的情况:
var http = function (callback) {
var bt = new BridgeTalk();
bt.target = 'bridge' ;
var s = '';
s += "if ( !ExternalObject.webaccesslib ) {\n";
s += " ExternalObject.webaccesslib = new ExternalObject('lib:webaccesslib');\n";
s += "}\n";
s += "var html = '';\n";
s += "var http = new HttpConnection('http://requestb.in/1mo0r1z1');\n";
s += "http.method = 'POST';\n";
s += "http.requestheaders = 'Content-Type, application/x-www-form-urlencoded'\n";
s += "http.request = 'abc=123&def=456';\n";
s += "var c=0,t='';for(var i in http){t+=(i+':'+http[i]+'***');c++;}t='BEFORE('+c+'):'+t;alert(t);\n"; // Debug: to see what properties and values exist on the http object
s += "http.response = html;\n";
s += "http.execute() ;\n";
s += "http.response;\n";
s += "var t='AFTER:';for(var i in http){t+=(i+':'+http[i]+'***');}alert(t);\n"; // Debug: to see what properties and values have been set after executing
bt.body = s;
bt.onResult = function (evt) {
callback(evt);
};
bt.onError = function (evt) {
callback(evt);
};
bt.send();
};
注意事项:
- 如果我尝试像上面的代码一样设置请求headers 属性,请求将失败。如果我将其注释掉,则请求成功。 requestheaders 的默认值未定义。
- 在请求成功后检查 http object,显示响应headers 属性设置为:"Connection, keep-alive,Content-Length, 2,Content-Type, text/html; charset=utf-8,Date, Wed, 24 Jun 2015 09:45:40 GMT,Server, gunicorn/18.0,Sponsored-By, https://www.runscope.com,Via, 1.1 vegur"。在请求执行之前,responseheaders 被设置为 undefined.
如果有人能帮我设置请求headers(特别是Content-Typeheader),我将不胜感激!
解决了!
设置content-typeheader的关键是设置http.mime属性如下:
s += "http.mime = 'application/x-www-form-urlencoded';\n";
另外,为了完整起见,您可以添加自己的自定义 header,如下所示:
s += "http.requestheaders = ['My-Sample-Header', 'some-value'];\n";
(原来 headers 是一个数组,其格式为 [key1, value1, key2, value2, .......])