在R上使用JSON正文发布POST请求的请求 [英] POST encripted request with JSON body on R

查看:145
本文介绍了在R上使用JSON正文发布POST请求的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R将加密的请求发布到 API .

I am trying to use R to post an encrypted request to an API.

具体是/v3/orders/请求.

它需要使用API​​ keysecret,以及递增的nonce.

It requires the use of an API key and secret, as well as an increasing nonce.

使用openssljsonlitehttr库:

主体必须为JSON编码:

The body has to be JSON encoded:

book<-"btc_eth"
side<-"sell"
major<-"0.1"
price<-"100"
type<-"limit"

Payload<-toJSON(data.frame(book=book,side=side,major=major,price=price,type=type))

它还需要一个使用 sha256 加密的signature构造的授权标头.

It also requires an authorization header constructed with an sha256 encrypted signature.

N<-NONCE() # "1503033312"

method<-"POST"

Path<-"/v3/orders/"

Signature<-sha256(paste0(N,method,Path,Payload),secret)

header<-paste0("Bitso ",key,":",N,":",Signature)

最后,请求应如下所示:

Finally the request should look like this:

url<-"https://api.bitso.com/v3/orders/"

r<-POST(url, body = Payload, add_headers(Authorization=header))

之前,我已经可以将带有空有效负载的请求发布到此API,但是此调用发送了不受支持的媒体类型错误,这与我对Paylod进行JSON编码的方式有关.

I have been able to post requests with an empty payload to this API before, but this call sends unsupported media type error, something about the way I'm JSON encoding the paylod is causing this.

此处中,有关于如何放置此请求的Ruby和PHP示例.

There's Ruby and PHP examples on how to place this request here.

推荐答案

因此,我终于能够发送请求.

So, I finally was able to send the request.

我必须感谢科林·费伊(Colin Fay)对如何消除括号的回应.

I have to thank Colin Fay for his response on how to eliminate the brackets.

问题是,标头必须使用不带括号的JSON主体构造,但必须使用自动JSON编码将主体作为列表发送,如下所示:

The thing was, the header had to be constructed with the unbracketed JSON body, but the body had to be sent as a list with automatic JSON encoding as following:

NC<-NONCE()

mthd<-"POST"

Pyld<- toJSON(data.frame(book=book,side=side,major=major,price=price,type=type))

Pyld <- gsub("\\[|\\]", "", Pyld)

body<-list(book=book,side=side,major=major,price=price,type=type)

url<-"https://api.bitso.com/v3/orders/"

Pth<-"/v3/orders/"

hdr<-paste0("Bitso ",ky,":",NC,":",sha256(paste0(NC,mthd,Pth,Pyld),scrt))

r<-POST(url, body = body, add_headers(Authorization=hdr),encode="json",verbose())

这篇关于在R上使用JSON正文发布POST请求的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆