Willy D
Willy D

Reputation: 1

Why Oanda order request gives an error message?

I am trying to send an order request to Oanda, but I receive the following error message: {"errorMessage":"Unable to parse JSON body."}. I have been working on the problem for a week, but no solution was found. I checked and rechecked all Oanda API requirements and everything seems correct. It seems like the Authentication is fine and when I change the POST method to GET and change the code a bit, then I receive all trades history from my account. I am not interested in the V20 Python wrapper and I would rather code everything from scratch. Based on the message, it looks like I am sending an incorrect order request and the reason might be either improper use of the Requests library or issue with the parameters. Any input how to make the code to work will be greatly appreciated. Thank you.

import requests

domain = 'api-fxpractice.oanda.com'
access_token = 'TOKEN'
account_id = 'ACCOUNT_ID'
Pair = "EUR_USD"

url = "https://" + domain + "/v3/accounts/" + account_id + "/orders"
headers = {"Authorization" : "Bearer "  + access_token}
params = {
      "type": "MARKET",
      "instrument": "EUR_USD",                        
      "units": "100",
      "timeInForce": "FOK",
      "positionFill": "DEFAULT"
         }
RequestData = requests.post(url, headers = headers, params = params)
print(RequestData.text)`

Upvotes: 0

Views: 495

Answers (1)

martingeorgiu
martingeorgiu

Reputation: 44

It looks like API requires JSON in body, so to achive this, import json library: import json

For request change params = params to data = json.dumps(params) which will send data in POST body as JSON. Full line of request will be:

RequestData = requests.post(url, headers = headers, data = json.dumps(params))

Upvotes: 1

Related Questions