Closed ah2014 closed 3 years ago
I found the solution. params
should be passed to session.post
function as json
and not as data
Here are the changes that I made to requester.py:
def makeRequest(self, method, endpoint, params=None, returnValue=None, jsonData=None):
"""Make a request to a given API endpoint
Parameters
----------
method : str
Specify POST or GET request
endpoint : str
URL endpoint oof API (Does not include base URL)
params : dict
Dictionary of parameters to be passed with request
jsonData : dict
Dictionary of parameters to be passed with request as json data
Returns
-------
Response : Response
A requests response object
"""
URL = self.APIMainURL + endpoint
if method == "POST":
return self.post(URL, params, jsonData)
elif method == "GET":
return self.get(URL, params)
else:
raise Exception(f"Invalid request method: {method}")
def post(self, URL, params=None, jsonData=None):
"""Make a POST request to a given API endpoint
Parameters
----------
URL : str
Full URL endpoint of API
params : dict
Dictionary of parameters to be passed with request
jsonData : dict
Dictionary of parameters to be passed with request as json data
Returns
-------
Response : Response
A requests response object
"""
try:
return self.session.post(URL, params, json=jsonData)
except Exception as err:
print(err)
It should be called like this:
params = {
'account_id': account_id,
'security_id': security_id,
'order_type': 'buy_quantity',
'order_sub_type': 'market',
'time_in_force': time_in_force,
'quantity': 1,
'limit_price': limit_price
}
self.TradeAPI.makeRequest("POST", "orders", jsonData=params)
I get a 400 response with "quantity is invalid." message when trying to place a market buy order. Does anybody have a similar problem?
Here is the response: