1

I am trying to create a market order on Oanda using httr::POST and am receiving a status code 415 error and the order is not being created. No expert here so please help me see what I am doing wrong.

Documentation from Oanda on API requests can be found here: https://developer.oanda.com/rest-live-v20/order-ep/#collapse_endpoint_2

Mark
  • 7,785
  • 2
  • 14
  • 34
rlangton
  • 11
  • 2
  • OP don't put your answer in the question! But good on you for solving your problem yourself! :-) – Mark Aug 17 '23 at 08:02

1 Answers1

0

After a health amount of googling and trial here is what works in case anyone else needs it. Note the content_type_json() inside POST:

URL_Orders<-'https://api-fxpractice.oanda.com/v3/accounts/{AccountID}/orders'
#replace {AccountID} with the account ID for the account (no {}) Token<-{Token} #fill with API Token generated from Oanda (no {})

#create the body for the request req = list(   order = list(instrument="EUR_USD",
               units="1000", 
               type="MARKET",
               timeInForce="FOK",
               positionFill="DEFAULT"
               ) )

#format body as JSON body<-toJSON(req, auto_unbox = T, pretty = FALSE)

#post request Test<-httr::POST(URL_Orders, 
                 content_type_json(),
                 add_headers("Authorization" = paste("Bearer ", Token)), 
                 body=body,
                 encode = "form",
                 verbose(data_out = TRUE, data_in = TRUE, info = TRUE)) 

- OP

Mark
  • 7,785
  • 2
  • 14
  • 34