Skip to content

Latest commit

 

History

History
74 lines (65 loc) · 1.74 KB

HOW-TO.md

File metadata and controls

74 lines (65 loc) · 1.74 KB

Http module

var http = require("http");

Get string from URL

http.getString("http://httpbin.org/get").then(function (r) {
    // Argument (r) is string!
}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});

Get JSON from URL

http.getJSON("http://httpbin.org/get").then(function (r) {
    // Argument (r) is JSON!
}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});

Get Image from URL

http.getImage("http://www.google.com/images/errors/logo_sm_2.png").then(function (r) {
    // Argument (r) is Image!
}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});

Get response status code

http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
    // Argument (response) is HttpResponse!
    var statusCode = response.statusCode;

}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});

Get response headers

http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
    for (var header in response.headers) {
        console.log(header + ":" + response.headers[header]);
    }

}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});

Get response content

http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
    // Argument (response) is HttpResponse!
    // Content property of the response is HttpContent!
    var str = response.content.toString();
    var obj = response.content.toJSON();
    var img = response.content.toImage();

}).fail(function (e) {
    // Argument (e) is Error!
    console.log(e);
});