|
#1
|
|||
|
|||
|
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);
});
});
Any idea what I missed, its a json array, base64 encoded, sent as application/json data via POST. Thanks, Deano |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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() );
});
});
}
Deano |
|
#5
|
||||
|
||||
|
Awesome, glad to hear and thanks for the contribution!
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|