• Online Demo
  • 30-day Free Trial
  #1  
Old 02-06-2011, 11:48 PM
DeanoC DeanoC is offline
Junior Member
 
Join Date: Jan 2011
Posts: 10
Default API call from server side java script

Hi,
I'm trying to make an API call from server side java script (node.js), the php docs say use json, are a little fuzzy on the exact requirements (content type, etc?).
Code:
	var postbuf = new Buffer(256);
	var getCurrency = { 'key': 'KEY', 'formatcurrency': true, 'amount': amount, 'pretty' : false };
	postbuf.write( JSON.stringify(getCurrency) );

	var blestaapi = http.createClient(80, 'bla.com' );
	
	var request = blestaapi.request('POST', '/api.php', {'host': 'blah.com', Content-Length': 256, 'Content-Type':'application/json' });
	request.write( postbuf.toString('base64'), 0, 'base64' );
	request.end();
	request.on('response', function (response) {
	  response.setEncoding('base64');
		console.log( JSON.stringify( response.headers) );
	  response.on('data', function (chunk) {
	    console.log('BODY: ' + chunk);
	  });
	});
Some headers are returned (run via PHP, but contect type is html and no data is ever sent).

Any idea what I missed, its a json array, base64 encoded, sent as application/json data via POST.

Thanks,
Deano
Reply With Quote
  #2  
Old 02-07-2011, 05:41 AM
Paul's Avatar
Paul Paul is offline
Blesta Developer
 
Join Date: Apr 2006
Location: Wherever I go, there I am.
Posts: 1,464
Send a message via AIM to Paul Send a message via MSN to Paul
Default

Is this an http server that runs off of javascript? www.nodejs.org?

Interesting

I don't think any of our devs are familiar with nodejs, but perhaps Cody can help.
__________________
Paul
Misc Stuff Guy

Blesta
Professional Billing
www.blesta.com
Reply With Quote
  #3  
Old 02-07-2011, 08:20 AM
DeanoC DeanoC is offline
Junior Member
 
Join Date: Jan 2011
Posts: 10
Default

Yeah node.js, its really nice and fast server architecture. All in javascript and async, my main website is written using it. Worse case I could write a php adaptor (takes in my formatted data, convert to php post data, send to api.php and vice versa), but obviously would prefer to do it directly.

As its a general server program it sometimes quite low level, which for example is why I need the exact POST details expected by api.php.

Thanks,
Deano
Reply With Quote
  #4  
Old 02-08-2011, 08:57 AM
DeanoC DeanoC is offline
Junior Member
 
Join Date: Jan 2011
Posts: 10
Default

After some late nights, figuring it all out, I have success!
For anybody else wanted to do so in future, here is a node.js function that calls getCurrency and returns the data in a json array ready to use.

Just replace KEY and SITE with your versions, and you should get logged to the console the blesta API response. Change the getCurrency array to any other API call should also work

Code:
priceFromBlesta = function( amount ) {
	
	var getCurrency = { 'key': 'KEY', 'formatcurrency': true, 'amount': amount, 'pretty' : false };
	var php_array = JSON.stringify(getCurrency);
	var postbuf = new Buffer(php_array);

	var blestaapi = http.createClient(80, 'SITE' );
	var based = postbuf.toString('base64');																											
	var otherbuf = '------------------------------933fde73d12f\r\nContent-Disposition: form-data; name="json"\r\n\r\n' +
			based+';}';

	var request = blestaapi.request('POST', '/api.php', {
				'host': 'SITE', 
				'Content-Length': otherbuf.length,
				'Content-Type':'multipart/form-data; boundary=----------------------------933fde73d12f' });

	request.write( otherbuf );
	request.end();

	request.on('response', function (response) {
	  response.setEncoding('binary');
	  response.on('data', function (chunk) {
			var recievebuf = new Buffer(chunk.toString(), 'base64');
	  
	    console.log('BODY:' + recievebuf.toString() );
	  });
	});
}
Thanks,
Deano
Reply With Quote
  #5  
Old 02-08-2011, 04:05 PM
Paul's Avatar
Paul Paul is offline
Blesta Developer
 
Join Date: Apr 2006
Location: Wherever I go, there I am.
Posts: 1,464
Send a message via AIM to Paul Send a message via MSN to Paul
Default

Awesome, glad to hear and thanks for the contribution!
__________________
Paul
Misc Stuff Guy

Blesta
Professional Billing
www.blesta.com
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 12:40 AM.