Force JSON response from API (for JSONP call)

101 views
Skip to first unread message

Blair McMillan

unread,
Jan 6, 2015, 4:50:27 PM1/6/15
to pollevery...@googlegroups.com
I'm trying to retrieve poll results in JSON via javascript. However, due to CORS I have to use JSONP to do the request. Due to the nature of JSONP, you can't specify header values (to be able to specify that the request only accepts JSON).

Because I can't specify to receive a JSON response, I get the full HTML page back (what you see when you visit that page in the browser)


Ideally I would like to be able to hit something like http://api.polleverywhere.com/multiple_choice_polls/poll_id/results?type=json

The following works:
curl -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Basic <snip>" "http://api.polleverywhere.com/multiple_choice_polls/poll_id/results"
As does:
curl -H "Accept: application/json" -H "Authorization: Basic <snip>" "http://api.polleverywhere.com/multiple_choice_polls/poll_id/results"
But this does not (it returns HTML):

Is there an API URL I can hit that will always return JSON regardless of the headers sent?

Thanks.

Paul Poll Everywhere

unread,
Jan 6, 2015, 5:03:40 PM1/6/15
to pollevery...@googlegroups.com
Try : http://api.polleverywhere.com/multiple_choice_polls/poll_id.json

You could also try http://www.polleverywhere.com/multiple_choice_polls/fTa9i4LV1x6vgae/results.json
But that would require authentication. You probably don't want to be giving JavaScript code your password. Anyone looking at the page would have full access to your account.

But please note: anything not documented at http://api.polleverywhere.com may change without notice. Practically speaking it doesn't change often. But it is important to keep it in mind.

HTH,
Paul

Blair McMillan

unread,
Jan 6, 2015, 5:23:35 PM1/6/15
to pollevery...@googlegroups.com
Wonderful thank you!

I assumed there was probably some way to set the response type, but couldn't find where it was listed. The only information that I could find was at http://api.polleverywhere.com

Thanks again!

sup...@abraxuslighting.co.uk

unread,
Apr 8, 2015, 7:16:24 AM4/8/15
to pollevery...@googlegroups.com
Hello,

Has there been a change in the api recently? I was using a python script with requests to gather responses, but came back a month later to having the same working script broken. The requests are now redirected [to http i assume], even though the headers are requiring json.

I ask here, because I mended it, but am now using the .json extension to force the json response, and would like to know if this is be made 'guaranteed' permanent. As far as anything is anyway.

BR
Mark

Paul Poll Everywhere

unread,
Apr 8, 2015, 6:33:08 PM4/8/15
to pollevery...@googlegroups.com
Hi Mark,

There are frequent changes to parts of the API, though the vast majority are backwards compatible. There was a bug recently where JSON requests to certain endpoints would return 406 unless they ended with ".json". This was not intended and has been fixed. I suspect this was the problem you had.

The API documented at http://api.polleverywhere.com/ is fairly stable. But we don't many guarantees about any particular API being permanent. If you need guarantees around an API, it can be done on a case-by-case basis. Just contact http://www.polleverywhere.com/contact/enterprise to discuss the details.

Cheers,
Paul
Reply all
Reply to author
Forward
0 new messages