Search code examples
javascriptparse-platformcorshttprequestparse-cloud-code

HTTP request through Parse Cloud code


So I am trying to get the cloud code to do an aftersave event that pings another location to do some data saving.

It has some data that needs to be sent as well.

Parse.Cloud.afterSave("ArtPiece", function(request) {
var artistURL = 'http://www.gallery-admin-dev.iartview.com/Gilmans/imageSave.php';

var pieceName = "Robert Rauschenberg Quote";
var url = 'http://www.gilmancontemporary.com/wp-content/uploads/2015/06/Delicate_Garden.The_Bear.48x42.jpeg';

Parse.Cloud.httpRequest({
    url: artistURL,
    dataType : "text",
    crossDomain: true,
    type:"GET",
    data : {
      pieceName : pieceName,
      url : url
    },

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

});

The code does not give an error, or any warnings. Me saving/creating entries works fine.

Cors is already handled, as I can successfully make this call with an ajax script.

Any clue why this is not sending? Can I not have data in a parse httpRequest? Can it not be crossdomain?

Alternatively Is there a way to test this code outside of the Parse Cloud???


Solution

  • The way you call httpRequest function is not correct in cloud. You also do not need to worry about CORS as you are not working in a browser environment. Try this instead:

    Parse.Cloud.httpRequest({
        url: artistURL,
        params: {
          pieceName : pieceName,
          url : url
        },
        success: function(httpResponse) {
            response(httpResponse.text);
        },
        error: function(httpResponse) {
            response('Request failed with response code ' + httpResponse.status)
        }
    });
    

    To test this outside cloud environment you can use the curl command. It will be something like:

    curl -G -v "http://www.gallery-admin-dev.iartview.com/Gilmans/imageSave.php" --data-urlencode "pieceName=Robert Rauschenberg Quote,url=http://www.gilmancontemporary.com/wp-content/uploads/2015/06/Delicate_Garden.The_Bear.48x42.jpeg"