Script for Adobe Creative Suite/Indesign – Http Request’s GET POST with Javascript

For a couple of days I work to chance information with web services using javascript at Indesign.

The first try was using HttpConnection from bridge. In this ocasion the post method dont work fine and I discard this idea.

the second try was use Javascript and this post talk about exacly this code.

Primarilly, I start using a well know code you can find at google. A function called getUrl(). this implementation was a little slow because the code get just one byte at once when it read the answer from socket. You can solve this at this code changing a couple lines but it’s just a function. without any object orientation facility.

The code above use OO and represent my initial test. Feel free for use, reproduce and changing this code. If you can give me your opinio… I’m glady with it.

function httpconn(method,ssl){
// todo: implements test for set of methods
this._coveredMethods = [“GET”,”POST”];
this.method = (method     == undefined)?”GET”:method;
this.port     = (ssl         == undefined)?”80″:ssl;
this.init();
}
httpconn.prototype.setUrl=function(url){
this.url    =    url;
this._configureSocket();
return this;
}
httpconn.prototype.init=function(){
this.socket = new Socket;
this.socket.timeout = 15;
this._parsedparam = new Object();
this.params = new Object();
this.contentType = “Content-Type: application/x-www-form-urlencoded; charset=UTF-8”;
this.extraHeader = “”;
}
httpconn.prototype.clean=httpconn.prototype.init;
httpconn.prototype._configureSocket=function(){
var _urlsplit        = this.url.replace(/[http|https]*[://]*/,””);
_urlsplit            = _urlsplit.split(“/”);
this.host             = _urlsplit[0];
if (_urlsplit[1] != undefined ){
_urlsplit.shift();
this.path        = “/”+ _urlsplit.join(“/”);
}else{
this.path        = “/”;
}
return this;
}
httpconn.prototype._parseParam=function(){
var _parsedparam = “”;
for (i in this.params){
_parsedparam = _parsedparam + ( ( (_parsedparam.length!=0)?”&”:””)+i+”=”+String( this.params[i]) );
}
this._parsedparam.str = _parsedparam;
this._parsedparam.length = _parsedparam.length;
return this;
}
httpconn.prototype.addParam=function(name, value){
this.params[name] =value;
return this;
}
httpconn.prototype.setContentType=function(ct){
this.contentType = ct+”n”;
}
httpconn.prototype.addHeader=function(headerLine){
this.extraHeader+=headerLine+”n”;
}
httpconn.prototype.request=function(){
var _request = this.method;

if (this.method==”POST”)
_request +=” “+this.path+” HTTP/1.1n”;
else
_request +=” “+this.path+”?”+this._parsedparam.str+” HTTP/1.1n”;

_request +=”Host: “+this.host+”:”+this.port+” n” +
“User-Agent: Arizona Systemsn” +
“Accept: */*n”+
this.contentType;
_request+=this.extraHeader;
if (this.method==”POST”)
_request+=”Content-Length: “+this._parsedparam.length+”n”;

_request+=”Connection: keep-alivernrn”;

alert(“host:”+this.host+”n method:”+this.method+”n port:”+this.port+”n dados:”+this._parsedparam.str);
alert(“request:”+_request);
this.socket.listen(this.port);
this.socket.open(this.host+”:”+this.port);
this.socket.write(_request);
this.socket.write(this._parsedparam.str);
//    this.clean();
return this.socket.read();
}

To use this object you can make like this

//http = new httpconn(“POST”);
//var retorno = http.setUrl(“http://192.168.0.21/ws/teste.php”).addParam(‘teste’,’1′).addParam(‘nome’,’ivo’)._parseParam().request();
//alert(retorno);

Hope you enjoy.

10 thoughts on “Script for Adobe Creative Suite/Indesign – Http Request’s GET POST with Javascript

    • Thanks man. Can u share the code you produce?
      Did you observe we(adobe developer) don’t have much information.
      we need share more code/informations…
      []’s

  1. Curious, what (or how) didn’t work for Bridge’s HttpConnection POST method? It worked for us, though by the time I worked for company we used CS5 and now CS6. Might have been CS4 or older at some point, not sure.

    All we had to do (compared to a GET) was add the following before executing the request:

    var http = new HttpConnection(“some URL”) ;
    http.method = ‘POST’;
    http.request = thePostBodyContentHere;
    http.mime = ‘application/json’; //or appropriate MIME type

    By the way, thanks for sharing this alternative approach.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s