As the title says, I'm trying to run a job on parse.com from javascript (specifically an Appcelerator Android app) using the REST API. I'm using REST because this is just for diagnostics and I don't want to deal with trying to get the parse.com javascript API working in Appcelerator. The problem is I cannot get authenticated. If I don't pass in the authentication headers, I get the appropriate 401 authentication error, but if I do set them, I get "BAD REQUEST". I have gotten it working via cURL, so the URL is right, and parse responds to the call as expected. Here's my code:
var url = "https://api.parse.com/1/jobs/sendMail";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
alert("Received text: " + this.responseText);
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
alert(e.error);
},
timeout : 5000 // in milliseconds
});
var param = {"text":msg};
// Prepare the connection.
var auth = {"app":"sTnsthoeunotreallymyappIDbutabunchofcharactersESnecu","key":"8ll5thisisntreallymykeyeitherhMKqkYG"};
client.open("POST", url);
client.setRequestHeader("X-Parse-Application-Id",auth.app);
client.setRequestHeader("X-Parse-REST-API-Key",auth.key);
client.setRequestHeader("Content-Type","application/json");
// Send the request.
client.send(param);
Here is the request and response:
POST https://api.parse.com/1/jobs/sendMail HTTP/1.1
X-Parse-Application-Id: myappid
User-Agent:
X-Parse-REST-API-Key: myrestapikey
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Content-Length: 0
Host: api.parse.com
Connection: Keep-Alive
HTTP/1.1 401 Unauthorized
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Date: Sun, 22 Feb 2015 04:36:11 GMT
Server: nginx/1.6.0
Set-Cookie: _parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlMTY4MzY0NTZlOWQ3ZGRjZDJkOWQwMjA4MWZjNWViMTY%3D--ffc760efbe32aa80a5e6d369606361413433fa72; domain=.parse.com; path=/; expires=Tue, 24-Mar-2015 04:36:11 GMT; secure; HttpOnly
Status: 401 Unauthorized
WWW-Authenticate: Basic realm="Parse"
X-Content-Type-Options: nosniff
X-Runtime: 0.018320
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 24
Connection: keep-alive
{"error":"unauthorized"}