httpRequest

Http

Request object provides convenient methods to build a http request and send it to any server over internet.

var options = {"type":"POST", "url":"www.cloudpact.com"};

var httpReq = $m.httpRequest(options);

Syntax

        $m.httpRequest(options)

Parameters

options

object(required)

The options of the http request which contain the following properties.

  1. url - {Required. String}. The url for which the request has to be built.
  2. headers - {Optional. Object}. The headers to be set for the http request. Default value is {}.
  3. timeout - {Optional. Number}. The timeout of the http request in milliseconds. Default value is 30000.

Return Value

Request. {Object}.The request object containing the following methods,

setType

Sets the type of the request. Possible values are "GET" and "POST".

httpReq.setType("post" );

Syntax

        setType(type)

Parameters

type

string (required)

The type of the http request. It is either "GET" or "POST". The type is not case-sensitive.

        

Return Value

httpRequest - {Object}. this httpRequest object.

setTimeout

Sets the http timeout of the request.

httpReq.setTimeout(5000);

Syntax

        setTimout(time)

Parameters

time

number (required)

The timeout of the http request in milliseconds. Default value is 30000.

Return Value

httpRequest - {Object}. this httpRequest object.

setData

Sets the data to be passed with the request.

httpReq.setData("welcome to mowbly");

Syntax

        setData(data)

Parameters

data

string/object (required)

The string value or a key value pair (JSON) to be passed with the request.

Return Value

httpRequest - {Object}. this httpRequest object.

addHeader

Adds a header to the request.

httpReq.addPart("content-type", "text/html" );

Syntax

        addHeader(name, value)

Parameters

name

string (required)

The name of the header to be added.

value

string (required)

The value of the header.

         

Return Value

httpRequest - {Object}. this httpRequest object.

addPart

Adds a part to the request.

httpReq.addPart("name" ,"mowbly" );

Syntax

addPart(name, content [, contentType, fileName])

Parameters

name

string (required)

The name of the part to be added.

content

string / File (required)

The content of the part. Use getFile to create mowbly File object.

contentType

string (optional)

The content type of the part. Defaults to "application/octet-stream".

fileName

string (optional)

The fileName with which the file part has to be sent to server.Defaults to the name of the part.

        

Return Value

httpRequest - {Object}. this httpRequest object.

send

Sends the request to the server.

httpReq.send( function(response){

        if(response.code == 103) {

          $m.toast(JSON.stringify(response.result));

         } else {

           $m.toast(response.error.message);

         }

});

Syntax

send(fp_callback)

Parameters

fp_callback

function (optional)

The function to be called after the request has been executed. The function receives the response object which contains the following properties.

  1. code - {Number}. 0 if the request failed otherwise the http status code sent by the server.

  1. result - {Object}. The result contains the following properties,
  1. headers - {Object}. The response headers received from the server.
  2. data - {String}. The response data received from the server.

  1. error - {Object}. If code is 0, the error contains the properties,
  1. message - {String}. The error message.
  2. description - {String}. The error description in detail if any.