Http library
Provides functionality for HTTP protocol communication
This library can be initialized
var Http = require('http');
Create Http instantion
Http new Http ();
Constructor creates new instance of Http object. For one Http request you need one Http instance.
Note When http request is done, you have to release http object. The object is not reusable.
Open http connection and modify header
open (url, method);
String url - contains url of target destination. For example http://www.myserver.com:8080/some/path
Number method - method sets one of two http methods. POST_METHOD and GET_METHOD
addToHeader (key, value);
String key - header's property name
String value - header's property value
Function adds some pair key and value into http header. For example: Content-length: 2002
Send http data
ByteBuffer send();
Function sends http request without body and returns response as ByteBuffer object.
Note For Http response code retrieving, use getHttpCode().
ByteBuffer send (sourceData);
String|Array|ByteBuffer|File sourceData
Number send (sourceData, targetFile);
String|Array|ByteBuffer|File sourceData
File targetFile
Function sends http request with body and returns response as ByteBuffer object or number of http response code. When targetFile is specified, response from server will be saved to targetFile.
Note When data is an File object, file has to exist. Function automaticaly opens file for read and sends to the server.
send (sourceData, function(httpCode, data));
String|Array|ByteBuffer|File sourceData
Function function(httpCode, data) - function will be invoked when HTTP request will be complete.
javascript
send (sourceData, targetFile, function(httpCode));
String|Array|ByteBuffer|File sourceData
File targetFile
Function function(httpCode) - function will be invoked when HTTP request will be complete.
Functions send data to the server in asynchronous mode. It creates new thread for sending and receiving data. When http request is done, it calls specified function with parameters.
Number httpCode - is http response code.
Retrieve data from response header
Object getResponseHeaderFields();
Function returns Object which contains all header fields from response header. You can access to any field by object.fieldName. See on example bellow!
Http example - Sync and async HTTP
var Http = require('http');
var File = require('file');
var sys = require('system');
//how to retrieve some data
//create Http object
var httpConnection = new Http();
//create GET connection
httpConnection.open("http://www.someserver.com",httpConnection.GET_METHOD);
//add some property to request header
httpConnection.addToHeader("key","value");
//send request and retrieve response to byteBuffer
var byteBuffer = httpConnection.send();
//print http response code
sys.println("HttpCode:"+httpConnection.getHttpCode());
//print data
sys.println("Content:"+byteBuffer.toString());
//print some property from response header
var ar = httpConnection.getResponseHeaderFields();
//ar is javascript object
sys.println("");
for (key in ar){
sys.println(key+":"+ar[key]);
}
//how to send some data to server and retrieve response
httpConnection = new Http();
//create GET connection
httpConnection.open("http://www.m2mscript.com",httpConnection.POST_METHOD);
//response will be write to byteBuffer
byteBuffer = httpConnection.send('{"firstParam":"firstValue", "secondParam":["a1","a2"]}');
//how to send some file to server
sys.println("\r\nFile uploading to server\r\n");
var file = new File("a:/program.js");
if (file.exists()){
httpConnection = new Http();
httpConnection.open("http://www.m2mscript.com",httpConnection.POST_METHOD);
byteBuffer = httpConnection.send(file);
if (httpConnection.getHttpCode()==200) {
sys.println("file was uploaded!");
} else {
sys.println("something is wrong! response code is "+httpConnection.getHttpCode();
}
file.close();
}
sys.sleep(15000);
//how to load file from server
var targetFile = new File("a:/something.bin");
sys.println("\r\nFile downloading from server\r\n");
httpConnection = new Http();
httpConnection.open("http://www.m2mscript.com",httpConnection.GET_METHOD);
//null can be replaced by data in string, bytebuffer or file - data for request
byteBuffer = httpConnection.send(null,targetFile);
file.close();
if (httpConnection.getHttpCode()==200) {
targetFile = new File("a:/something.bin");
if (targetFile.exists()) {
sys.println("File size is "+targetFile.size());
}else {
sys.println("something is wrong! response code is "+httpConnection.getHttpCode();
}
}
sys.sleep(15000);
//how to do as async?
sys.println("async1");
httpConnection = new Http();
//create GET connection
httpConnection.open("http://misha.alarex.net",httpConnection.POST_METHOD);
var reqData = "ahoj"//'{"firstParam":"firstValue", "secodnParam":["a1","a2"]}';
//response will be write to byteBuffer
httpConnection.send(reqData,function(httpCode, data){
sys.println("HttpCode:"+httpCode);
sys.println("Data:"+data+"\r\n\r\n");
});
//wait 15 second
sys.sleep(15000);
sys.println("async2");
targetFile = new File("a:/something.bin");
httpConnection = new Http();
//create GET connection
httpConnection.open("http://www.m2mscript.com",httpConnection.POST_METHOD);
//response will be write to byteBuffer
httpConnection.send('{"firstParam":"firstValue", "secodnParam":["a1","a2"]}',targetFile,function(httpCode){
sys.println("HttpCode:"+httpCode);
var f = new File(fileName);
if (f.exists()){
sys.println("File size is "+f.size());
}
f.close();
});