dbs.83 dbs.83 - 3 months ago 17
Python Question

Python: Properly formatting JSON parameters into a proper http request with markit on demand api

I am having trouble formatting my request properly in order to use the markitondemand InteractiveChart API. How can I properly do this?

reference: http://dev.markitondemand.com/MODApis/

Here is an example of a proper request:

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


Here is my code constructing a request:

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)})


and here is the output when I log the resp.url :

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


What am I doing wrong here?

Answer

Your url, params and logic are wrong, what you want is:

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())

if we run the 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}]}

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

req_obj = {'Normalized': False,

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

resp = requests.get(url2, params={'parameters': json.dumps(req_obj)})

But it is as easy to just use the strings and forget dumps.

Comments