python - 如何使用标头和数据在 Python 中正确执行 http 请求
问题描述
我正在尝试根据此处编写的文档获取一些东西:
这是我的代码:
import requests
import json
post_data = {"exchange": "NSE",
"tradingsymbol": "INFY",
"transaction_type": "BUY",
"variety": "regular",
"product": "CNC",
"order_type": "MARKET",
"quantity": 1,
"price": 0,
"trigger_price": 0
}
header_data = {
'X-Kite-Version': '3',
'Authorization': 'token '+str(api_key)+":"+str(access_token),
'Content-Type': 'application/json'
}
r = requests.post('https://api.kite.trade/margins/orders',
data= post_data,
headers=header_data)
print(r.text)
但我收到了这个回复
{"status":"error","message":"invalid json","data":{},"error_type":"InputException"}
我想知道,我做错了什么。
解决方案
我没有尝试过,但看起来输入数据应该是一个列表/数组,而不是一个字典/对象。所以试试这个数据:
post_data = [
{
"exchange": "NSE",
"tradingsymbol": "INFY",
"transaction_type": "BUY",
"variety": "regular",
"product": "CNC",
"order_type": "MARKET",
"quantity": 1,
"price": 0,
"trigger_price": 0
}
]
推荐阅读
- google-sheets - 如何组合 2 个查询导致来自 Google 表格的单个查询
- javascript - 如何在 asp.core MVC React 应用程序中注册 JS 引擎?
- python - django.db.utils.IntegrityError:安装夹具时出现问题
- java - 包含自身作为元素的 ArrayList 的哈希码
- python - 如何根据特定键比较python字典
- c++ - 执行用 C++ 编写的 .exe 的问题(使用 mingw 编译器)
- python - 将 DataFrame 中的每个唯一值重塑为列
- php - $row 将在此返回什么
- regex - 我如何将变量多行 perl 正则表达式与不同的规则匹配
- c# - 如何有条件地从 ASP.NET Core 注册中删除控制器并添加到 ServiceCollection