Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"}
share|improve this question
    
You could try relaying your request through Runscope and then do the same with cUrl and then use Runscope to do a side by side comparison to try and identify why it's not working in your Android app. –  Darrel Miller Feb 22 at 0:26
    
Clients may add other default headers. U will need to turn on logging ( Wire & headers ) or look at server log to see exactly what's being sent . same headers n wire as curl and you will get same results –  Robert Rowntree Feb 22 at 2:37
    
I added the request and response to the question. I used Fiddler to get that - seems simpler than Runscope. However, when I use cURL (or ping) Fiddler doesn't show anything, so I am not sure what the differences are. I've installed the cURL client on Windows 7 - any idea why Fiddler wouldn't show me that traffic? I haven't found any answers with a bit of searching but I will keep looking. –  nasch Feb 22 at 4:50

1 Answer 1

Triggering background jobs via the REST-API requires using the Master-Key, not the REST-API key.

https://parse.com/docs/rest#backgroundjobs

share|improve this answer
    
Good catch, but I changed it to set the header "X-Parse-Master-Key" with the master key, and still get "Bad request". Note that this is different from setting no authentication headers, when I get "Unauthorized". So it appears rather than rejecting the credentials as incorrect, it's saying there is something malformed. –  nasch Feb 23 at 15:38
    
Hm, have you tried starting the job directly via the REST-API (using curl). From what you posted, there is no data sent to the API (Content-Length: 0), maybe somethings up there? If you can share the code for the job (simply remove all confidential parts) I can set it up in one of my apps to debug it. –  Björn Kaiser Feb 23 at 18:05
    
It works fine from cURL. I noticed that content length 0 as well, and there's also no user agent, but I don't know if either of those is the problem. I don't think what the job does really matters - it's not even being run. The issue is with getting a correct request from this mobile app. If you happen to have any familiarity with Appcelerator, calling a parse job from there might shed some light. But I don't think there's much crossover between parse.com and Appcelerator. BTW I was also trying to call mailgun directly to send an email and that gave me the same error. Any clues there? –  nasch Feb 23 at 18:44
    
One thing that's confusing me about this is I can't capture the cURL traffic. I've tried Fiddler, Charles, and MS Message Analyzer, and when I run the cURL command I see nothing going to parse.com in the traffic analyzers. –  nasch Feb 24 at 1:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.