Https library

Provides functionality for HTTPS protocol communication

This library can be initialized

var Https = require('https');

Create Https instantion

Https new Https ();

Constructor creates new instance of Https object. For one Https request you need one Https instance.

Note When https request is done, you have to release https object. The object is not reusable.

Open https connection and modify header

open (url, method);

String url - contains url of target destination. For example https://www.myserver.com/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 https header. For example: Content-length: 2002

Send https data

ByteBuffer send();

Function sends https request without body and returns response as ByteBuffer object.

Note For Https 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 https request with body and returns response as ByteBuffer object or number of https 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 HTTPS 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 HTTPS request will be complete.

Functions send data to the server in asynchronous mode. It creates new thread for sending and receiving data. When https request is done, it calls specified function with parameters.

Number httpCode - is https 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!

Https example - Sync and async HTTPS

var Https = require('https');
var File = require('file');
var sys = require('system');
//how to retrieve some data
//create Http object
var httpsConnection = new Https();
//create GET connection
httpsConnection.open("http://www.someserver.com",httpConnection.GET_METHOD);
//add some property to request header
httpsConnection.addToHeader("key","value");
//send request and retrieve response to byteBuffer
var byteBuffer = httpsConnection.send();
//print http response code
sys.println("HttpCode:"+httpsConnection.getHttpCode());
//print data
sys.println("Content:"+byteBuffer.toString());
//print some property from response header
var ar = httpsConnection.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
httpsConnection = new Https();
//create GET connection
httpsConnection.open("http://www.m2mscript.com",httpConnection.POST_METHOD);
//response will be write to byteBuffer
byteBuffer = httpsConnection.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()){
    httpsConnection = new Https();
    httpsConnection.open("http://www.m2mscript.com",httpsConnection.POST_METHOD);
    byteBuffer = httpsConnection.send(file);
    if (httpsConnection.getHttpCode()==200) {
        sys.println("file was uploaded!");
    } else {
        sys.println("something is wrong! response code is "+httpsConnection.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");

httpsConnection = new Https();
httpsConnection.open("https://www.m2mscript.com",httpsConnection.GET_METHOD);
//null can be replaced by data in string, bytebuffer or file - data for request
byteBuffer = httpsConnection.send(null,targetFile);
file.close();
if (httpsConnection.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 "+httpsConnection.getHttpCode();
    }
}
sys.sleep(15000);

//how to do as async?
sys.println("async1");
httpsConnection = new Https();
//create GET connection
httpsConnection.open("http://misha.alarex.net",httpConnection.POST_METHOD);
var reqData = "ahoj"//'{"firstParam":"firstValue", "secodnParam":["a1","a2"]}';
//response will be write to byteBuffer
httpsConnection.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");
httpsConnection = new Https();
//create GET connection
httpsConnection.open("http://www.m2mscript.com",httpConnection.POST_METHOD);
//response will be write to byteBuffer
httpsConnection.send('{"firstParam":"firstValue", "secondParam":["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();
    });

results matching ""

    No results matching ""