Search code examples
rrcurlhttrcoinbase-apigdax-api

R GDAX-API Delete Request


I am having trouble with DELETE requests in R. I have been successful in making GET and POST requests using the below code. Any help / pointers will be appreciated.

It will require an api.key, secret & passphrase from GDAX to work.

Here is my function:

library(RCurl)
library(jsonlite)
library(httr)
library(digest)

cancel_order <- function(api.key,
                         secret,
                         passphrase) {
  api.url <- "https://api.gdax.com"

  #get url extension----
  req.url <- "/orders/"

  #define method----
  method = "DELETE"

  url <- paste0(api.url, req.url)

  timestamp <-
    format(as.numeric(Sys.time()), digits = 13) # create nonce
  key <- base64Decode(secret, mode = "raw") # encode api secret

  #create final end point----
  what <- paste0(timestamp, method, req.url)

  #create encoded signature----
  sign <-
    base64Encode(hmac(key, what, algo = "sha256", raw = TRUE)) # hash

  #define headers----
  httpheader <- list(
    'CB-ACCESS-KEY' = api.key,
    'CB-ACCESS-SIGN' = sign,
    'CB-ACCESS-TIMESTAMP' = timestamp,
    'CB-ACCESS-PASSPHRASE' = passphrase,
    'Content-Type' = 'application/json'
  )
  ##------------------------------------------------
  response <- getURL(
    url = url,
    curl = getCurlHandle(useragent = "R"),
    httpheader = httpheader
  )
  print(rawToChar(response)) #rawToChar only on macOS and not on Win
}

The error I get is "{\"message\":\"invalid signature\"}" even though the same command will code and signature will work with GET & POST.

Ref: GDAX API DOCs


Solution

  • Ok. I took @mrflick's advise and pointed my connection to requestbin based on his feedback on a different but related question.

    After careful inspection, I realized that the my request for some reason was treated as a POST request and not a DELETE request. So I decided to replace the getURL function with another higher level function from RCurl for it to work.

    response <- httpDELETE(
      url = url,
      curl = getCurlHandle(useragent = "R"),
      httpheader = httpheader
    )
    

    Everything else remains the same. Apparently there never was an issue with the signature.

    I have added this function now to my unofficial wrapper rgdax

    EDIT::
    The unofficial wrapper is now official and on CRAN.