【问题标题】:How do I set http headers in Adobe Illustrator ExtendScript when using the BridgeTalk HttpConnection object?使用 BridgeTalk HttpConnection 对象时,如何在 Adob​​e Illustrator ExtendScript 中设置 http 标头?
【发布时间】:2015-06-24 10:04:55
【问题描述】:

我正在尝试从 Illustrator ExtendScript(通过 BridgeTalk)中发出 http post 请求,并且大部分情况下它都在工作。但是,不存在有关使用 HttpConnection 的文档,我正在尝试弄清楚如何设置 http-headers。 HttpConnection 对象同时具有 requestheaders 和 responseheaders 属性,所以我怀疑这是可能的。

默认情况下,post 请求使用 Content-Type 标头“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();
};

注意事项:

  1. 如果我尝试在上面的代码中设置 requestheaders 属性,请求将失败。如果我将其注释掉,则请求成功。请求标头的默认值未定义。
  2. 在请求成功后检查 http 对象,显示要设置的响应头属性:“Connection, keep-alive,Content-Length, 2,Content-Type, text/html; charset=utf-8,Date,格林威治标准时间 2015 年 6 月 24 日星期三 09:45:40,服务器,gunicorn/18.0,赞助商,https://www.runscope.com,Via,1.1 vegur"。在请求执行之前,responseheaders 被设置为 undefined。

如果有人可以帮我设置请求标头(特别是 Content-Type 标头),我将不胜感激!

【问题讨论】:

    标签: adobe-illustrator extendscript adobe-bridge


    【解决方案1】:

    解决了!

    设置content-type头的关键是设置http.mime属性如下:

    s += "http.mime = 'application/x-www-form-urlencoded';\n";
    

    为了完整起见,您可以添加自己的自定义标头,如下所示:

    s += "http.requestheaders = ['My-Sample-Header', 'some-value'];\n";
    

    (原来 headers 是一个数组,格式为 [key1, value1, key2, value2, .......])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2019-10-21
      • 2021-04-09
      相关资源
      最近更新 更多