How do I set http headers in Adobe Illustrator ExtendScript when using the BridgeTalk HttpConnection object?

426 views Asked by At

I am trying to make http post requests from within Illustrator ExtendScript (via BridgeTalk) and for the most part it is working. However, the documentation on using HttpConnection is non-existent and I am trying to figure out how to set http-headers. The HttpConnection object has both a requestheaders and responseheaders property so I suspect it is possible.

By default, the post requests are being sent with the Content-Type header "text/html", and I would like to override it so that I can use either "application/x-www-form-urlencoded" or "multipart/form-data".

Here is what I have so far:

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();
};

Things to note:

  1. If I try setting the requestheaders properties like in my code above, the request fails. If I comment it out, the request succeeds. The default value for requestheaders is undefined.
  2. Examining the http object after a successful request, shows the reponseheaders properties to be set to: "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". Before the request executes, the responseheaders is set to undefined.

If anyone could help me set the request headers (in particular the Content-Type header), I would be eternally grateful!

1

There are 1 answers

0
romiem On BEST ANSWER

Solved it!

The key for setting the content-type header is to set the http.mime property as follows:

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

Also for completeness, you can add your own custom headers as follows:

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

(It turns out the headers is an array which takes the format [key1, value1, key2, value2, .......])