Python: Properly formatting JSON parameters into a proper http request with markit on demand api
<p>I am having trouble formatting my request properly in order to use the markitondemand InteractiveChart API.  How can I properly do this?</p>

<p>reference: <a href="http://dev.markitondemand.com/MODApis/" rel="nofollow">http://dev.markitondemand.com/MODApis/</a></p>

<p>Here is an example of a proper request:</p>

<pre><code>http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart/json?parameters=%7B%22Normalized%22%3Afalse%2C%22NumberOfDays%22%3A10%2C%22DataPeriod%22%3A%22Day%22%2C%22Elements%22%3A%5B%7B%22Symbol%22%3A%22AAPL%22%2C%22Type%22%3A%22price%22%2C%22Params%22%3A%5B%22c%22%5D%7D%5D%7D
</code></pre>

<p>Here is my code constructing a request:</p>

<pre><code>import requests
import json        
url2 = 'http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart/json'
elements = [
        {
            'Symbol': 'GOOG',
            'Type': 'price',
            'Params': {'price': ['c']},
        }
    ]

req_obj = {
    'Normalized': 'false',
    'NumberOfDays': 3,
    'DataPeriod': 'Day',
    'Elements': elements
}

resp = requests.get(url2, params={'parameters': json.dumps(req_obj)})
</code></pre>

<p>and here is the output when I log the resp.url :</p>

<pre><code>http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart/json?parameters=%7B%22Elements%22%3A+%5B%7B%22Type%22%3A+%22price%22%2C+%22Params%22%3A+%7B%22price%22%3A+%5B%22c%22%5D%7D%2C+%22Symbol%22%3A+%22GOOG%22%7D%5D%2C+%22NumberOfDays%22%3A+3%2C+%22DataPeriod%22%3A+%22Day%22%2C+%22Normalized%22%3A+%22false%22%7D
</code></pre>

<p>What am I doing wrong here?</p>

<p>Your <em>url</em>, <em>params</em> and logic are wrong, what you want is:</p>

<pre><code>url2 = 'http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart/json'
elements = [
        {
            'Symbol': 'GOOG',
            'Type': 'price',
            "Params":["c"] # not 'Params': {'price': ['c']}
        }
    ]

req_obj = {"parameters": {'Normalized': 'false',

    'NumberOfDays': 3,
    'DataPeriod': 'Day',
    'Elements': elements
}}

from urllib import urlencode
resp = requests.get(url2, params=urlencode(req_obj))
print(resp.json())
</code></pre>

<p>if we run the code:</p>

<pre><code>In [12]: import requests

In [13]: import json

In [14]: url2 = 'http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart/json'

In [15]: elements = [
   ....:         {
   ....:             'Symbol': 'GOOG',
   ....:             'Type': 'price',
   ....:             "Params":["c"]
   ....:         }
   ....:     ]

In [16]: req_obj = {"parameters": {'Normalized': 'false',
   ....: 
   ....:     'NumberOfDays': 3,
   ....:     'DataPeriod': 'Day',
   ....:     'Elements': elements
   ....: }}

In [17]: from urllib import urlencode

In [18]: resp = requests.get(url2, params=urlencode(req_obj))


In [19]: print(resp.json())
{u'Positions': [0, 1], u'Dates': [u'2016-09-01T00:00:00', u'2016-09-02T00:00:00'], u'Labels': None, u'Elements': [{u'Currency': u'USD', u'Symbol': u'GOOGL', u'Type': u'price', u'DataSeries': {u'close': {u'maxDate': u'2016-09-02T00:00:00', u'max': 796.46, u'minDate': u'2016-09-01T00:00:00', u'values': [791.4, 796.46], u'min': 791.4}}, u'TimeStamp': None}]}
</code></pre>

<p>You can keep also the python Booleans etc.. calling dumps on just the inner data: </p>

<pre><code>req_obj = {'Normalized': False,

    'NumberOfDays': 3,
    'DataPeriod': 'Day',
    'Elements': elements
}

resp = requests.get(url2, params={'parameters': json.dumps(req_obj)})
</code></pre>

<p>But it is as easy to just use the strings and forget dumps.</p>

python json