App Development Company
App Development Rest API

How to call a REST API using Parse Cloud function? | App Development Company

REST API

Representational State Transfer (REST) is is an architectural style used for web development. It relies on a stateless, client-server, cacheable communications protocol. Perfomatix is an app development company that uses REST API for product developments.Parse Rest Api

Call a REST API from Parse.com Cloud Function

Parse cloud function allows sending HTTP requests to Server using Parse.Cloud.httpRequest functions. This function returns a Promise that will be resolved on a successful HTTP status code otherwise, the Promise will be rejected.

An example REST API code with GET method.

Parse.Cloud.httpRequest({
  url: 'https://www.myexampleurl.com'//put your rest url
}).then(function(httpResponse) {
  console.log(httpResponse.text);//success call back
}, function(httpResponse) {
   'response code ' + httpResponse.status);//Error call back
});

You can set/send  parameters using Params only after set URL. You can pass it JSON format.

Parse.Cloud.httpRequest({
  url: 'https://www.example.com/myfunction',
  params: {
    "studentName": 'MYName',
   "Age":"21"
  }
}).then(function(httpResponse) {
  console.log(httpResponse.text);
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

or you can send it as a raw String like this:

Parse.Cloud.httpRequest({
  url: 'https://www.example.com/myfunction',
 params: 'studentName=Nandan'
}).then(function(httpResponse) {
  console.log(httpResponse.text);
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

You can also set HTTP Header using Header attribute and Content-Type of the request like this:

Parse.Cloud.httpRequest({
  url: 'https://www.example.com/myfunction',
 params: 'studentName=MyName',
 headers: {
    'Content-Type': 'application/json;charset=utf-8'
  }
}).then(function(httpResponse) {
  console.log(httpResponse.text);
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

You can send POST method by method, attribute body of POST can be set body attribute in JSON format like this:

Parse.Cloud.httpRequest({
  url: 'https://www.example.com/myfunction',
 method: 'POST',//define your method
 body: {
    "name": 'MY Name',
    "age": '22'
  }
}).then(function(httpResponse) {
  console.log(httpResponse.text);
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

Limitation

Parse.com allow only process 8 concurrent HTTP requests per Cloud Code request, and additional requests will be queued up.

Perfomatix | App Development Company

Would you like to know more about our process of developing web and mobile apps?

Feel free to drop in a note for us right now. Talk to our experts now and let us be your innovation partner!

Fast Forward Innovation with Perfomatix. 

Perfomatix | Product Engineering Services Company