我一直在研究用js编写的DeGiro (unofficial) API,并试图复制R中的某些功能。
但是我在使用httr发送使用JSON.stringify()的POST时遇到问题,js中的完整代码如下:
return fetch(`https://degiro.quotecast.vwdservices.co/CORS/${vwdSession.sessionId}`, {
method: 'POST',
headers: {Origin: 'https://trader.degiro.nl'},
body: JSON.stringify({
controlData: `req(${issueId}.BidPrice);req(${issueId}.AskPrice);req(${issueId}.LastPrice);req(${issueId}.LastTime);`}),})
我在R中重新创建有效的js代码的尝试如下:
bodyprices = list(
controlData = c(paste0("req(",issueId,".BidPrice)"),
paste0("req(",issueId,".AskPrice)"),
paste0("req(",issueId,".LastPrice)"),
paste0("req(",issueId,".LastTime)",";")
))
res = httr::("https://degiro.quotecast.vwdservices.co/CORS/29393-23293-20202", encode = "json", add_headers(Origin = "https://trader.degiro.nl"), body = bodyprices)
# The body sent looks like this: {"controlData":["req(450221092.BidPrice)","req(450221092.AskPrice)","req(450221092.LastPrice)","req(450221092.LastTime);"]}
返回200 OK,但没有ID数据。我还尝试了另一种方式来填充json主体,以尝试复制js函数:
bodyprices = toJSON(list(controlData = paste0("req(",issueId,".BidPrice)", ";req(",issueId,".AskPrice)",";req(",issueId,".LastPrice)", ";req(",issueId,".LastTime);")))
bodyprices = gsub("\\[|\\]", "", bodyprices)
res = httr::POST("https://degiro.quotecast.vwdservices.co/CORS/29393-23293-20202", body = bodyprices, add_headers(Origin = "https://trader.degiro.nl"))
# The body sent looks like this: {"controlData":"req(450221092.BidPrice);req(450221092.AskPrice);req(450221092.LastPrice);req(450221092.LastTime);"}
此尝试还返回200 OK,但没有价格数据。无法确定发生了什么以及如何格式化请求。
非常感谢您帮助我们理解问题所在以及解决方法。