2

How do I POST to external url in Parse.com from the cloud?

I looked around but there seems to be nothing in Parse.com's forums, and the generic javascript solution involves creating a form object:

var form = document.createElement("form");

which is obviously not the solution I need because I need to submit the url from the server (cloud code.)

What I'm looking for is something like this:

var url = "http://servername/v1/submit?";
var params = "lm_form=33333&lm_key=55555&fld_28226=name"
url = url + params;
var response = submitToUrl(url);

So the question is, what does submitToUrl look like?

EDIT: following Wain's answer I'm using this:

Parse.Cloud.httpRequest({
    url: url,
    method: 'POST',

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

with the url being the url + params from above. It triggers the success method but with 400 error code.

Submitting the same url through the browser window works fine. What am I missing?

1
  • Can't you use an Ajax request? Commented Aug 4, 2015 at 6:46

1 Answer 1

1

Something like:

Parse.Cloud.httpRequest({
    url: ...,
    method: 'POST',
    header:{
        ...
    },
    body:{
        ...
    }
}

(Either using promises or callbacks to handle the response)

It isn't clear from the question wether your parameters should be in the URL or the body, for POST it would usually be the body...

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, could you add a couple of parameters to the body section? Just so I don't need to worry about the syntax.
Using a url like this: 'servername/v1/submit?fld_28226=test&fld_28227=000000000&fld_30064=carrier&fld_29603=19.9' without adding header or body submits the url, but comes back with 400 response code. Submitting the same url through the browser address line works fine. Any idea why?
A browser does a GET, not a POST. Are you sure you really want to be POSTing?
Yes, I want to do a POST. But in any case I tried and changed it to GET with the same result: the error block is called, with 400 response code.
Good stuff, just to add that if you do add body content be sure to add a Content-Type header
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.