I'm trying to connect to a trading API and I've been successful making GET requests, but when I try to make a POST request I keep getting error messages from the API. I've searched the internet and forums for an answer, but all similar questions have ended unresolved.
Here's the R script I'm using:
# Call the required packages
library(ROAuth)
library(RJSONIO)
library(XML)
# Set your application keys
cKey <- 'xxxx'
cSecret <- 'xxxx'
oKey <- 'xxxx'
oSecret <- 'xxxx'
# Set the API endpoint
tkURL <- "https://api.tradeking.com/v1/accounts/xxxxxxxx/orders/preview.xml"
# Create the OAuth connection - this is straight from the ROAuth documentation on CRAN
credentials <- OAuthFactory$new(consumerKey = cKey,
consumerSecret = cSecret,
oauthKey = oKey,
oauthSecret = oSecret,
needsVerifier = FALSE,
signMethod='HMAC')
# Update the connection so the handshake is TRUE
credentials$handshakeComplete <- TRUE
# Create FIXML
doc <- newXMLDoc()
root <- newXMLNode('FIXML', namespaceDefinitions = 'http://www.fixprotocol.org/FIXML-5-0-SP2',
newXMLNode('Order', attrs=c(TmInForce='0', Typ='1', Side='1', Acct='xxxxxxxx'),
newXMLNode('Instrmt', attrs=c(SecTyp='CS', Sym='FAS')),
newXMLNode('OrdQty', attrs=c(Qty='1'))), doc=doc)
newFIXML = saveXML(doc, encoding='UTF-8')
newFIXML
# Post order
response <- credentials$OAuthRequest(tkURL, method = "POST", newFIXML)
response
When I run this, I get this response:
"<response id=\"-3491729f:14e4f104ddc:7f50\">\n\t
<type>Error</type>\n\t
<name>ScriptFailure</name>\n\t
<description></description>\n\t
<path>/v1/accounts/xxxxxxxx/orders/preview.xml</path>\n</response>"
attr(,"Content-Type")
charset
"application/xml" "UTF-8"
I talked with the API guy and he gave these suggestions, but said that he couldn't help with the R code:
I am not familiar with R so I'm probably not going to be much help on the code part. As for the response id, it seems that the headers were incorrectly sent in the body of the request along with the fixml. Also, the fixml seems to be encoded, it should not be. This is more like xml or text and the content type should reflect that in your request so that it does not try to encode it. Instead of Content-Type:application/x-www-form-urlencoded, you should try Content-Type: text/xml. Here (pasted below) is what we got from your request:
Headers:
Host:api.tradeking.com Content-Length:668 Accept:*/* Content-Type:application/x-www-form-urlencoded
Body:
=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3CFIXML%20xmlns%3D%22http%3A%2F%2Fwww.fixprotocol.org%2FFIXML-5-0-SP2%22%3E%0A%20%20%3COrder%20TmInForce%3D%220%22%20Typ%3D%221%22%20Side%3D%221%22%20Acct%3D%22xxxxxxxx%22%3E%0A%20%20%20%20%3CInstrmt%20SecTyp%3D%22CS%22%20Sym%3D%22FAS%22%2F%3E%0A%20%20%20%20%3COrdQty%20Qty%3D%221%22%2F%3E%0A%20%20%3C%2FOrder%3E%0A%3C%2FFIXML%3E%0A&oauth_consumer_key=xxxx&oauth_nonce=xxxx &oauth_signature_method=HMAC-SHA1&oauth_timestamp=1435846001&oauth_token=xxxx&oauth_version=1.0&oauth_signature=xxxx%2xxxx%3D
How would I go about fixing this in the code?
I think you need to put the FIXML code after the URL like this:
Or did you find a better solution?