我正在尝试从Illustrator ExtendScript(通过BridgeTalk)中发出http发布请求,并且大多数情况下它正在运行。但是,关于使用HttpConnection的文档是不存在的,我试图弄清楚如何设置http-headers。 HttpConnection对象同时具有requestheaders和responseheaders属性,所以我怀疑它是可能的。
默认情况下,发布请求的内容类型标题为“text / html”,我想覆盖它以便我可以使用“application / x-www-form-urlencoded”或“多部分/格式数据”。
这是我到目前为止所做的:
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();
};
注意事项:
如果有人可以帮我设置请求标题(特别是Content-Type标题),我将永远感激不尽!
答案 0 :(得分:2)
解决了!
设置content-type标头的关键是设置http.mime属性,如下所示:
s += "http.mime = 'application/x-www-form-urlencoded';\n";
另外,为了完整起见,您可以按如下方式添加自己的自定义标题:
s += "http.requestheaders = ['My-Sample-Header', 'some-value'];\n";
(事实证明,标题是一个采用格式[key1,value1,key2,value2,.......]的数组)