Python:使用markit on demand api正确地将JSON参数格式化为正确的http请求

时间:2016-09-04 00:37:09

标签: python json

我无法正确格式化请求以使用markitondemand和InteractiveChart API。我该怎么做呢?

参考:http://dev.markitondemand.com/MODApis/

以下是正确请求的示例:

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

以下是构建请求的代码:

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

这是我记录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

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

你的 url params 和逻辑错了,你想要的是:

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

如果我们运行代码:

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

你也可以保留python Booleans等。只调用内部数据上的转储:

req_obj = {'Normalized': False,

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

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

但是使用字符串并忘记转储也很容易。

答案 1 :(得分:0)

你的网址终点(看起来很简单)应该是:

url2 = 'http://dev.markitondemand.com/MODApis/Api/v2/InteractiveChart

然后,提出您的请求:

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

如果需要,requests将正确处理?和任何&