Start a new topic

HTTP error registering Device for Push

Hey,



Using backbone library and titanium - for 2 days (or maybe more) i am getting a http error when i am trying to register a device for Push.





The Script:



var url = (Kinvey.API_ENDPOINT + '/push/kid_PTYXXXXX/register-device').toString();

var xhr = Ti.Network.createHTTPClient({

onload: function(e) {

console.log(this.responseText);

alert('success');

},

onerror: function(e) {

// this function is called when an error occurs, including a timeout

console.log(e);

alert('error');

},

timeout:5000 /* in milliseconds */

});

xhr.setRequestHeader('Authorization', 'Kinvey ' + Kinvey.getActiveUser()._kmd.authtoken);

xhr.setRequestHeader('Content-Type', 'application/json');

xhr.open("POST", url);

xhr.send({

'platform' : 'ios',

'deviceId' : token

});



Each time i get this:



{

[INFO] : code = 401;

[INFO] : error = "HTTP error";

[INFO] : source = "[object TiNetworkHTTPClient]";

[INFO] : success = 0;

[INFO] : type = error;

[INFO] : }





Any idea whats causing this?



Best, Nico

I might be mistaken, but I do believe the `xhr.open` call should be made *before* setting request headers.
Hey Mark, thanks for your quick reply. - Sadly this does not solve the problem.





My guess:

Before updating to the new Titanium SDK 3.3.0 i received a lot of conenction errors calling the Kinveybackend. (just ios, android was fine)

After the update calling the backend works fine on ios, too. So my guess is that something changed regarding the http protocol or something like that.



related:

http://docs.appcelerator.com/titanium/release-notes/?version=3.3.0.RC#http_client



Could you maybe run a test on your site using the new sdk? Cause i can't find any other error :neutral_face: (The other stuff like receiving the device token and so on works like a charm)





Best, Nico



I can see in the logs both the *Authorization* and *Content-Type* header are not set. If you tried moving the `xhr.open` call to above `xhr.setRequestHeader`, and that didn’t work - I’d try asking around on the Titanium forums, as this seems a Titanium problem.



For me, moving the open call to the top results in making network requests successfully.
Hmm just to be clear:





with moving above you mean



xhr.open("POST", Kinvey.API_ENDPOINT + '/push/kid_XXXXXX/register-device');

xhr.setRequestHeader('Authorization', 'Kinvey ' + Kinvey.getActiveUser()._kmd.authtoken);

xhr.setRequestHeader('Content-Type', 'application/json');





instead of





xhr.setRequestHeader('Authorization', 'Kinvey ' + Kinvey.getActiveUser()._kmd.authtoken);

xhr.setRequestHeader('Content-Type', 'application/json');

xhr.open("POST", Kinvey.API_ENDPOINT + '/push/kid_XXXXXX/register-device');





right?
Yes. Either way, the headers somehow aren’t added to the request, therefore Kinvey returns a 401.
Hey,



sadly i can't get it to work right now ... :(





QUestion: What does the request on the Kinvey-side? Just entering the token into the user-object? (If so, i propably can just save it by my own? )



Best, Nico
Solution:



As we are using a node.js Service for several other things, i make the registerForPush POST Request on the node.js side with the informations coming from the application.



This way it works fine and as expected. :)
Login or Signup to post a comment