Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfaisal/9751003 to your computer and use it in GitHub Desktop.
Save rfaisal/9751003 to your computer and use it in GitHub Desktop.
//sending back whatever is receving as a body and parameter
exports.post = function(request, response) {
response.send(statusCodes.OK, { request_body: request.body, request_param: request.query });
};
exports.put = function(request, response) {
response.send(statusCodes.OK, { request_body: request.body, request_param: request.query });
};
var access_token = 'eyJhbGciOi...';
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(30000);
xhr.onload=function() {
Ti.API.info('Protected Resource by POST: '+this.responseText);
};
xhr.onerror= function() {
Ti.API.info('Error response: '+this.responseText);
};
xhr.open('POST', 'https://my_resource_auth_server.azure-mobile.net/api/auth');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("X-ZUMO-AUTH", access_token); // this is the magic line
xhr.send(JSON.stringify({
"user" : {
"name" : "faisal",
"interested_in" : ["male", "female"],
"current_look" : {
"photo_url" : "",
"identifier": {
"type" : "1",
"brand" : "2",
"color" : "3"
}
}
}
}));
{
"request_body": {
"user": {
"name": "faisal",
"interested_in": [
"male",
"female"
],
"current_look": {
"photo_url": "",
"identifier": {
"type": "1",
"brand": "2",
"color": "3"
}
}
}
},
"request_param": {}
}
var access_token = 'eyJhbGciOiJI...';
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(30000);
xhr.onload=function() {
Ti.API.info('Protected Resource by PUT: '+this.responseText);
};
xhr.onerror= function() {
Ti.API.info('Error response: '+this.responseText);
};
xhr.open('PUT', 'https://my_resource_auth_server.azure-mobile.net/api/auth?id=123');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("X-ZUMO-AUTH", access_token); // this is the magic line
xhr.send(JSON.stringify({
"user" : {
"name" : "faisal",
"interested_in" : ["male", "female"],
"current_look" : {
"photo_url" : "",
"identifier": {
"type" : "1",
"brand" : "2",
"color" : "3"
}
}
}
}));
{
"request_body": {
"user": {
"name": "faisal",
"interested_in": [
"male",
"female"
],
"current_look": {
"photo_url": "",
"identifier": {
"type": "1",
"brand": "2",
"color": "3"
}
}
}
},
"request_param": {
"id": "123"
}
}
@pdichone
Copy link

Hi Faisal,

Thanks again for sharing this with me. I am having issues understanding some stuff ( my apologies for my ignorance ). I have tried to implement at least part of this into my project and getting authentication error now. I am guessing the JSON package I am sending is not formatted well - here's my json to be sent looks like:

 var params = {
    'UserId' : USER_ID, //test id for now
    'Salutation' : "Sir",
    'FirstName' : "Suzana", //bundleOne.firstName,
    'LastName' : "Machava", //bundleOne.lastName,
    'MiddleName' : "Ruti", //bundleOne.middleName,
    'NickName' : "Anita",
    'BirthDate' : null, //bundleOne.doB || null, // DateTime type
    'Company Name' : "Mycompnay",
    'RegDate' : "None",
    'Gender' : null, //bundleOne.gender || null, // int
    'IsPregnant' : "No", //bundleOne.prego,
    'IsNursing' : "No", //bundleOne.nursing,
    'WorkPhone' : "123445", //bundleOne.phone,
    'WorkPhoneExt' : "125", //bundleOne.phone,
    "PhoneNumber" : "384747",
    'Address' : "", //bundleOne.address,
    'Address2' : "", //bundleOne.address,
    'City' : "Spokenite", //bundleOne.city,
    'State' : "WA", //bundleOne.state || null, //int
    'ZipCode' : "99123", //bundleOne.zipCode,
    'EmailAddress' : 'paulo@paulo.com',
    'Occupation' : 'Nurse',
    'CountryofResidence' : 'Sofala',
    'Ethnicity' : "Blue", //bundleTwo.ethnicity || null, //int
    'ModifiedDate' : 'Datehere',
    'ModifiedBy' : 'Me',
    'AcceptedTerms' : 'yes'

  };

I notice that in your example you have something very different. In my case, I am wanting to update data, so needless to say, I'd have to use "PUT" instead of "POST" as you mentioned in your example?

The other thing is the id you are appending in the url. In my case I am actually including it in the JSON I am going to send, which is wrong. But how to construct the URL is another problem I am having, I think. For instance, my endpoint url is:

    https://mywebserivicesomething.azure-mobile.net/tables/

and then in my code I append a table name, in this case "PatientInformation" table name. Where do I append the userID?

I know there might be too many questions. I am just stuck trying to understand what's going on, and I hope you can help me.

Thank you Faisal.

@rfaisal
Copy link
Author

rfaisal commented Mar 25, 2014

You should pass the id as the url parameter. So, it should look like:

https://mywebserivicesomething.azure-mobile.net/tables/PatientInformation?UserId=1

And the UserId will be available in the server at request.parameters.UserId

if you want the id to be part of your request body you can access it the server at request.body.UserId

Authentication error has to do with permissions. Please paste what is in your PatientInformation.json file.

@rfaisal
Copy link
Author

rfaisal commented Mar 25, 2014

Humm, I see the problem now.

You are using the tables endpoint whereas the example was for api endpoints. Here is what you are gonna do:

To Add:
Everything should be the same, except the body will be simple JSON (key-value pair, where key is the column name) and the end point would be something like: https://my_resource_auth_server.azure-mobile.net/tables/todoitem

To Update:
You have to use HTTP PATCH instead of PUT. And the url should end with /id, for example: https://my_resource_auth_server.azure-mobile.net/tables/todoitem/123 where 123 is the id.

Here is the working example (I have tested it, let me know if it doesn't work):
https://gist.github.com/rfaisal/9773273

BTW, I still think Authentication error is due to permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment