Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

How do I set http headers when using the BridgeTalk HttpConnection object?

$
0
0

Hi,

 

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!


Thank you


UPDATE:

Using the ExtendScript SDK, I have discovered the responseheaders property is an array, of this format: ['Connection', 'keep-alive', 'Content-Length', 2, 'Content-Type', 'text/html' .........]

I have tried to set the requestheaders to the same format (I have tried both ['Content-Type', 'application/x-www-form-urlencoded'] and ['Connection', 'keep-alive', 'Content-Length', 2, 'Content-Type', 'application/x-www-form-urlencoded' .........]) with no luck




Viewing all articles
Browse latest Browse all 12845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>