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.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.

Need help?

We are Perfomatix. We specialize in building highly scalable APIs and Mobile apps and we also have strong expertise in IoT appsVirtual Reality apps, and Augmented Reality appsGet in touch with us to find out how we can help in shaping your disruptive idea into a prototype, MVP and finally into a killer product. Visit our success stories section to find out more about some of the startups which made it big with us.


captcha

X