Please enable JavaScript to view this site.

Navigation: Advanced topics > Programming topics > REST API Connections > RestConnection class > RestConnection Methods

requestJson

Scroll Prev Next More

Do authenticated request and return parsed JSON

Syntax

 

requestJson( resource, method, payload, headers, urlParams)

Arguments

 

resource

string. Resource address within API. resource is added to the REST API Connection URL.

For example, when REST API Connection URL is https://service.com/api

and resource is /orders, the resulting URL where request will be sent is https://service.com/ap/orders

method

string. The HTTP method. In most cases it should be GET or POST. More rarely used methods are PUT, DELETE, HEAD. Consult your API manual for more info.

 

payload (optional)

array. Associative array of parameters passed in the request body. payload should only be used with POST or PUT methods. See also HttpRequest  class

 

headers (optional)

array. Associative array of HTTP headers to be sent to the server. See also HttpRequest  class

Return value

 

Returns array representing parsed JSON response when request was successful.

Returns false otherwise. Use lastError() function to get the error details. If the server returns an error, lastError() returns the whole server response, header and the body.

Example

 

dynamic rconn = null, request = null, result = null;
rconn = CommonFunctions.getRESTConn();
result = rconn.requestJSON( "/resource", "GET" );
if (!result) {
 MVCFunctions.Echo("Error occurred. Description: ");
       MVCFunctions.Echo(rconn.lastError());
} else {
 MVCFunctions.Echo("Request successful. Result: ");
 MVCFunctions.print_r(result);
}