user4297677
user4297677

Reputation:

How-to correctly make a Parse.com Parse.Cloud.httpRequest from the client-side?

I'd like to make a http request from my cloudcode that gets called on my clientside.

Upvotes: 2

Views: 3354

Answers (1)

garrettmac
garrettmac

Reputation: 8585

I found this a bit confusing at first so hopefully this helps.

In your Cloud Code main.js

Parse.Cloud.define("POSTfromCloud", function(request, response) {

   //runs when Parse.Cloud.run("POSTfromCloud") on the client side is called
   Parse.Cloud.httpRequest({
        method: "POST",
        headers: {
          "X-Parse-Application-Id": "[PARSE_APP_ID]",
          "X-Parse-REST-API-Key": "[PARSE_REST_ID]",
          "Content-Type": "application/json"
       },

       //adds a new class to my parse data
       url: "https://api.parse.com/1/classes/newPOSTfromCloudClass/",


       body: {
               "newPOSTfromCloudClass": {"key1":"value1","key2":"value2"}
             },

       success: function (httpResponse) {
                console.log(httpResponse.text);
                response.success(httpResponse);
       },
       error:function (httpResponse) {
                console.error('Request failed with response code ' + httpResponse.status);
                response.error(httpResponse.status);
       }

    });  //end of Parse.Cloud.httpRequest()

});

On your client side. This can be placed anywhere in any language, just use the Parse.Cloud.run to call the matching Parse.Cloud.define you placed in the cloud. You use the

Parse.Cloud.run('POSTfromCloud', {}, {
        success: function(result) {
          console.log("Posted a new Parse Class from Cloud Code Successfully! :"+ JSON.stringify(result))
        },
        error: function(error) {
        console.log("Oops! Couldn't POST from Cloud Code successfully..  :"+ error)
        }
      });
    }

Your Result: Assuming your POSTing enter image description here

(here if you want to delete this new object your url would append the object id like so /newPOSTfromCloudClass/60j1uyaBt )

Know it doesnt have to be a httpRequst cloud function. You can do "anything" in the define and run functions.

NOTE: Also seen my other related question on passing params in this here

Upvotes: 5

Related Questions