Issue with GET request from within Titanium

528 views
Skip to first unread message

MDPauley

unread,
Jan 11, 2012, 8:34:46 PM1/11/12
to Hyperpublic API Developers, m...@hyperpublic.com
Using a HTTP connection in Titanium I make a GET request to :

https://api.hyperpublic.com/api/v1/places?client_id=yRAeAlfIm6SztevrbPWnRjPB3J8V9qEWYCfXZijU&client_secret=tUF3TNnFMqDHG1SsTuntfkYZKcHKkeWkNmTgyM0J&callback=&phone_number=2122292217&limit=20

and this is the response received:

{"error":"Internal error. We're looking into it."}

When I use Chrome in OSX 10.7.2 the request returns 1 result:

[{"phone_number":"212 229 2217","website":"http://
hyperpublic.com/","perma_link":"http://hyperpublic.com/places/
4dd535cab47dfd026c000001/profile","tags":
["camillia","jonathan","eric","doug","YOU","Jordan","Mike","zane","jeff"],"locations":
[{"name":"416 West 13th st, New york, NY
10014","lon":-74.006974,"city":"New
york","country":"US","postal_code":"10014","province":"NY","lat":
40.740574,"address_line1":"416 West 13th
st","address_line2":null}],"display_name":"Hyperpublic
Headquarters","properties":[],"category":[{"name":"Buildings &
Workplaces","id":"4e28ae87643bce0004000001"},
{"name":"Offices","id":"4e28ae87643bce0004000014"},{"name":"Tech
Startups","id":"4e28ae87643bce0004000015"}],"images":
[{"src_thumb":"http://s3.amazonaws.com/prestigedevelopment/beta/
image_photos/4dd535cab47dfd026c000002/thumb.png?
1296938636","src_small":"http://s3.amazonaws.com/prestigedevelopment/
beta/image_photos/4dd535cab47dfd026c000002/small.png?
1296938636","src_large":"http://s3.amazonaws.com/prestigedevelopment/
beta/image_photos/4dd535cab47dfd026c000002/square.png?
1296938636"}],"id":"4dd535cab47dfd026c000001","object_type":"Place"}]

From the error returned is it possible to narrow down in the server
logs what the issue is?

Doug Petkanics

unread,
Jan 12, 2012, 12:07:02 PM1/12/12
to hyperpublic-a...@googlegroups.com, m...@hyperpublic.com
Hi,

It looks like Titanium's HTTP Client doesn't set a required header - "Content-Type" - unless you pass in the parameters in the xhr.send() method. You can solve this on the titanium side by one of two ways - either set the header explicitly, or pass your parameters in send() instead of as part of the URL. The two code examples are below...

Option 1

var xhr = Ti.Network.createHTTPClient();

xhr.timeout = 1000000;

xhr.open("GET","https://api.hyperpublic.com/api/v1/places?client_id=yourid&client_secret=yoursecret&phone_number=2122292217&limit=20");

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

xhr.onload = function(){

  ...

}

xhr.send();


Option 2

var xhr = Ti.Network.createHTTPClient();

xhr.timeout = 1000000;

xhr.open("GET","https://api.hyperpublic.com/api/v1/places");

xhr.onload = function(){

  ...

}

xhr.send({client_id:"your client id", client_secret: "your client secret", phone_number: "2122292217", limit:20});



Both should work. We'll evaluate on our end whether or not this should even be required in the first place and whether we should handle it server side.

-Doug
--
Doug Petkanics | @petkanics
Co-founder at Hyperpublic

Michael Pauley

unread,
Jan 12, 2012, 2:56:36 PM1/12/12
to hyperpublic-a...@googlegroups.com
Thanks,

I tried Option 2 originally with offers and when I received the error listed below that was when I moved on to Option 1. Option 2 is my preferred method.


Places: Find, show & create all work

Offers; I receive this error: {"error":"Only admin users can perform this action"}

Categories: list & show both return a 404 error


Should I post my module on github now so you can test or how would you like to procede?


Mike

Doug Petkanics

unread,
Jan 12, 2012, 5:57:26 PM1/12/12
to hyperpublic-a...@googlegroups.com
Ok, after more digging it became clear that Titanium will convert any GET request to a POST request if you pass any parameters into the send() method. Therefore, for Hyperpublic methods that require a GET request (like the categories endpoints and the find endpoint), you will have to pass your parameters in as part of the URL string. That, combined with setting the Content-Type header, should get the job done.


We'll also look into implementing handling the special titanium case on our side so that in the future people calling from Titantium don't have to set the Content-Type.

-Doug

Michael Pauley

unread,
Jan 12, 2012, 6:05:32 PM1/12/12
to hyperpublic-a...@googlegroups.com
No problem, I'll get the changes made and update the repo tonight. 

Thanks!

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages