2

I'm trying to pass JSON directly in a URL to communicate with the Ecobe API. No matter how I try to format the data, it gets escaped in the URL. Requests used to allow you to pass a config parameter in the call and that had an option to shut off encoding, but it has been removed (so the answer in this thread no longer works). The only reference I can see in the docs is to something in requests.defaults but I can't figure out how to set that. Because of this, my URL has all the JSON formatting URL-escaped instead of looking like what the API wants:

GET https://api.ecobee.com/1/runtimeReport?format=json&body={"startDate": "2010-01-01","endDate": "2010-01-02","columns": "zoneHVACmode,zoneCalendarEvent","selection":{"selectionType":"thermostats","selectionMatch": 123456789012"}}

Additionally, I've tried enough shotgun approaches (including the one in this thread) I'm not sure what's the best/ most efficient way to do this if it did work, so here's (an abbreviated version of) my current code in case there's a better option:

self.api_url = 'https://api.ecobee.com/%s?format=json&%s'
data = {
    'startDate': start_date.strftime('%Y-%m-%d'),
    'endDate': end_date.strftime('%Y-%m-%d'),
    'columns': 'auxHeat1,compCool1,outdoorHumidity,zoneAveTemp,zoneCoolTemp,zoneHeatTemp',
    'includeSensors': 'true',
    'selection': self.selection_info
}
endpoint = 'runtimeReport'
params_json = simplejson.dumps(params)
response = requests.get(self.api_url % (endpoint, params_json), headers=self._get_headers())
Community
  • 1
  • 1
Tom
  • 22,301
  • 5
  • 63
  • 96
  • using requests.post will let you pass a data dictionary – Joran Beasley Jul 29 '13 at 22:56
  • Sorry, I'm not sure exactly how you're communicating with the API from your code, but why not use a `urllib2` request and pass it the JSON data, with the JSON encoding? http://stackoverflow.com/questions/3290522/urllib2-and-json. Edit: Oh, I see, you're using Python requests. A post should do it, as @JoranBeasley mentioned. – bbill Jul 29 '13 at 22:57
  • But the API wants it as a GET request. Will edit to clarify the problem @jsalonen. – Tom Jul 29 '13 at 23:39

1 Answers1

0

Doesn't look like it can be done any longer. Every URL gets passed through requote_uri in utils.py. And unless I'm missing something, the fact this API wants JSON with spaces in a GET parameter is a bad idea.

Tom
  • 22,301
  • 5
  • 63
  • 96