[英]Looking to send Python API request using cURL
I am looking to send a POST request using Python to the OANDA API to open an order. 我希望使用Python将POST请求发送到OANDA API以打开订单。 They do not have a Python wrapper and use cURL, so I have had to try and convert from cURL to Python.
他们没有Python封装程序并使用cURL,因此我不得不尝试将cURL转换为Python。 I have done so using https://curl.trillworks.com/ -- but converting this next one does not work.
我已经使用https://curl.trillworks.com/做到了-但是转换下一个无效。
You can view the OANDA API documentation here, under the first Green POST tab - http://developer.oanda.com/rest-live-v20/order-ep/ 您可以在此处的第一个绿色POST标签下查看OANDA API文档-http: //developer.oanda.com/rest-live-v20/order-ep/
Here is what I am working with. 这是我正在使用的。 This first block specifies the order details.
第一个块指定订单详细信息。 In this case, a market order in the EUR_USD instrument with a quantity of 100 units and a time in force equalling "fill or kill" :
在这种情况下,EUR_USD工具中的市场订单数量为100个单位,有效时间等于“填充或杀死”:
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer SECRET TOKEN" \
-d "$body" \
"https://api-fxpractice.oanda.com/v3/accounts/{ACCOUNT-NUMBER}/orders"
Converted into Python: 转换成Python:
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer SECRET TOKEN',
}
data = '$body'
response = requests.post('https://api-fxpractice.oanda.com/v3/accounts/{ACCOUNT-NUMBER}/orders', headers=headers, data=data)
As you can see, I believe there is a formatting error somewhere in the "body=$" portion, but I am not entirely sure. 如您所见,我相信“ body = $”部分中存在格式错误,但我不确定。 I simply get a 400 error, "invalid values."
我只是得到一个400错误,“无效值”。
If you're sending data in JSON format, you should pass them into json
argument instead of data
( explanation , method ). 如果您以JSON格式发送数据,则应将其传递给
json
参数而不是data
( 解释 , 方法 )。
import requests
headers = {
# 'Content-Type': 'application/json', # will be set automatically
'Authorization': 'Bearer SECRET TOKEN',
}
body = {
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
response = requests.post('https://api-fxpractice.oanda.com/v3/accounts/{ACCOUNT-NUMBER}/orders',
headers=headers, json=body)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.