【问题标题】:Httr header returns invalid character '-' in numeric literalHttr 标头在数字文字中返回无效字符“-”
【发布时间】:2016-01-23 06:35:09
【问题描述】:

我正在使用 httr 访问 Stockfighter 的 API,这是一款 CTF 风格的交易游戏。

GET 函数可以正常工作,但是当我尝试使用标头中的 API 密钥进行身份验证时,它似乎无法正常工作。这是我的place_order 函数

place_order <- function(acct, exchange, stock, price, qty,
                        direction = c("buy", "sell"),
                        type = c("limit", "market", "fill-or-kill",
                                 "immediate-or-cancel")) {
# Place a stock order
  if (!exists("key")) stop("No authorisation key defined.")
  direction <- match.arg(direction)
  type <- match.arg(type)
  bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
               "price" = price, "qty" = qty, "direction" = direction,
               "orderType" = type)
  rurl <- paste(burl, "/venues/", exchange, "/stocks/", stock, "/orders",
               sep = "")
  r <- POST(rurl, body = bdy, add_headers(`X-Starfighter-Authorization` = key))
  return(content(r))
}

这是我得到的回报:

$ok
[1] FALSE

$error
[1] "invalid character '-' in numeric literal"

JSON 似乎没有正确转义破折号。

这是我发布到 httpbin 而不是 API 时得到的响应:

$args
named list()

$data
[1] ""

$files
named list()

$form
$form$account
[1] "RB34256134"

$form$direction
[1] "buy"

$form$orderType
[1] "limit"

$form$price
[1] "12400"

$form$qty
[1] "100"

$form$symbol
[1] "FOOBAR"

$form$venue
[1] "TESTEX"


$headers
$headers$Accept
[1] "application/json, text/xml, application/xml, */*"

$headers$`Accept-Encoding`
[1] "gzip, deflate"

$headers$`Content-Length`
[1] "751"

$headers$`Content-Type`
[1] "multipart/form-data; boundary=------------------------49a2e51c0c6926dd"

$headers$Host
[1] "httpbin.org"

$headers$`User-Agent`
[1] "libcurl/7.43.0 r-curl/0.9.4 httr/1.0.0.9000"

$headers$`X-Starfighter-Authorization`
[1] "OBFUSCATED KEY HERE"


$json
NULL

$origin
[1] "1.125.48.185"

$url
[1] "http://httpbin.org/post"

我觉得这可能是一个非常愚蠢的简单错误,但我无法解决。

编辑:

这是使用requestsjson 完美运行的python 方法。

def sf_post(path, data, key, **kwargs):
    base_url = "https://api.stockfighter.io/ob/api/"
    r = requests.post("%s/%s" % (base_url, path), data = data, headers = {'X-Starfighter-Authorization': key}, **kwargs)
    return(r)    

def order(self, price, qty, direction, order_type):
            data = dict(account = self.account, venue = self.exchange, symbol = self.symbol, price = price, qty = qty,
                        direction = direction, orderType = order_type)
            r = sf_post("%s/orders" % self.rurl, data = json.dumps(data), key = self.key)
            return r.json()

    cph = Stock("CPH", "EXMBEX", account = "ACCOUNTCODEHERE", key = os.environ.get("SF_KEY"))

    cph.order(5000, qty = 100, direction = "buy", order_type = "limit")
    {u'direction': u'buy', u'ok': True, u'ts': u'2016-01-24T00:35:21.148877285Z', u'fills': [{u'price': 4694, u'ts': u'2016-01-24T00:35:21.148881279Z', u'qty': 100}], u'originalQty': 100, u'orderType': u'limit', u'symbol': u'CPH', u'venue': u'EXMBEX', u'account': u'SSM90915021', u'qty': 0, u'id': 754, u'totalFilled': 100, u'open': False, u'price': 5000}

【问题讨论】:

  • X-Starfighter-Authorization 是变量吗?如果是这样,请合法地重命名。如果是字符串,请正确引用。
  • Alistaire - 我尝试了 " quotes ' 引号和我这个例子中的引号都具有相同的结果。
  • 你确定不应该发送 json 吗?
  • @hadley 你是指bdy 列表吗?我试过了,但现在它正在发送"json: cannot unmarshal array into Go value of type string"
  • 您当前没有发送 json。你需要POST(..., encode = "json")

标签: r http httr


【解决方案1】:

我认为我可能错过了一些愚蠢的东西,正如@hadley 在 cmets 中指出的那样。我需要在我的 POST 调用中添加 encode = "json"。为了后代,这里是更新的功能代码:

place_order <- function(acct, exchange, stock, price, qty,
                        direction = c("buy", "sell"),
                        type = c("limit", "market", "fill-or-kill",
                                 "immediate-or-cancel")) {
  if (!exists("key")) stop("No authorisation key defined.")
  direction <- match.arg(direction)
  type <- match.arg(type)
  bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
               "price" = price, "qty" = qty, "direction" = direction,
               "orderType" = type)
    rurl <- paste(burl, "venues/", exchange, "/stocks/", stock, "/orders",
                 sep = "")
  r <- POST(rurl, body = bdy,
            add_headers(`X-Starfighter-Authorization` = key),
            encode = "json")
  return(content(r))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2019-10-24
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多