我正在尝试使用以下内容将一些数据点发送到web api。(不确定要不要共享密钥,所以我留了一部分)。
library(jsonlite)
library(httr)
url = "https://api.marketcycles.online/api/CycleScanner"
t <- 1:101
val <- 2*sin(2*pi*t/25) + 5*cos(2*pi*t/50)
val[1:3]
body.list <- list(datapoints = val)
query.list <- list(api_Key = "wtt****")
res <- POST(url = url, query = query.list, body = body.list,
encode = "json")
a <- content(res, as = "text")
b <- fromJSON(a)
b[[2]]这会导致错误"Object reference not set to an object instance“
下面的curl命令可以工作(我已经编辑掉了大部分数据点)
curl -X POST --header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-d '[5.458,5.8064,6.018,....5,5.458]' \
'https://api.marketcycles.online/api/CycleScanner?api_Key=wtt****'对POST命令的工作有什么建议吗?
谢谢你的回答hrbrmstr。非常感谢。我尝试了以下几种方法
httr::POST(
url = "https://api.marketcycles.online/api/CycleScanner",
httr::content_type_json(),
httr::accept_json(),
encode = "json",
body = jsonlite::toJSON(val),
query = list(
amplitudeMulti = "1.0",
bartelsLimit = "49",
minCycleLength = "5",
maxCycleLength = "300",
sortByStrength = "true",
includeSpectrum = "false",
humanReadableText = "false",
api_Key = "wtt****")
)使用和不使用带有content_type_jason()的行,并在这两种情况下都收到以下错误
b[[1]]
[1] "The request contains an entity body but no Content-Type header. The
inferred media type 'application/octet-stream' is not supported for this
resource."
b[[2]]
[1] "No MediaTypeFormatter is available to read an object of type
'Double[]' from content with media type 'application/octet-stream'."
b[[3]]
[1] "System.Net.Http.UnsupportedMediaTypeException"据我所知,当主体是一个列表时,encode()提供列表元素的格式。当它不是的时候,我不清楚该怎么做。我查看了httr源代码,发现有一个未记录的函数body_config,但不确定是否在POST命令中使用此函数。有什么建议可以解决这个问题吗?
发布于 2018-09-26 19:24:09
我没有API键(你没有分享你的是100%正确的),但是他们的Swagger API Docs有点像他们期望的输入。
我在他们提供的在线Swagger测试器中使用了一个带有该端点的假API键(知道它会失败,但仍然显示curl行),并填充了默认值,以全面了解端点所期望的内容,它返回了以下内容(格式由我设置,返回一行):
curl
-X POST
--header 'Content-Type: application/json'
--header 'Accept: application/json'
-d '[5.458,5.8064,6.018,6.0702,5.9472,5.6409,5.1517,4.4888,3.6699,2.7207,1.6732,0.5646,-0.5646,-1.6732,-2.7207,-3.6699,-4.4888,-5.1517,-5.6409,-5.9472,-6.0702,-6.018,-5.8064,-5.458,-5,-4.4632,-3.8794,-3.2798,-2.6929,-2.143,-1.6488,-1.2225,-0.8695,-0.5879,-0.3695,-0.2007,-0.0633,0.0633,0.2007,0.3695,0.5879,0.8695,1.2225,1.6488,2.143,2.6929,3.2798,3.8794,4.4632,5,5.458,5.8064,6.018,6.0702,5.9472,5.6409,5.1517,4.4888,3.6699,2.7207,1.6732,0.5646,-0.5646,-1.6732,-2.7207,-3.6699,-4.4888,-5.1517,-5.6409,-5.9472,-6.0702,-6.018,-5.8064,-5.458,-5,-4.4632,-3.8794,-3.2798,-2.6929,-2.143,-1.6488,-1.2225,-0.8695,-0.5879,-0.3695,-0.2007,-0.0633,0.0633,0.2007,0.3695,0.5879,0.8695,1.2225,1.6488,2.143,2.6929,3.2798,3.8794,4.4632,5,5.458]'
'https://api.marketcycles.online/api/CycleScanner?amplitudeMulti=1.0&bartelsLimit=49&minCycleLength=5&maxCycleLength=300&sortByStrength=true&includeSpectrum=false&humanReadableText=false&api_Key=aaaa'这表明文档在建议datapoints值被命名时是在撒谎。事实并非如此。这很可能是导致您的错误的原因。
我通过curlconverter运行原始的、生成的curl命令行,它以httr模板的形式提供了以下内容:
httr::VERB(
verb = "POST",
url = "https://api.marketcycles.online/api/CycleScanner",
httr::add_headers(Accept = "application/json"),
body = "[5.458,5.8064,6.018,6.0702,5.9472,5.6409,5.1517,4.4888,3.6699,2.7207,1.6732,0.5646,-0.5646,-1.6732,-2.7207,-3.6699,-4.4888,-5.1517,-5.6409,-5.9472,-6.0702,-6.018,-5.8064,-5.458,-5,-4.4632,-3.8794,-3.2798,-2.6929,-2.143,-1.6488,-1.2225,-0.8695,-0.5879,-0.3695,-0.2007,-0.0633,0.0633,0.2007,0.3695,0.5879,0.8695,1.2225,1.6488,2.143,2.6929,3.2798,3.8794,4.4632,5,5.458,5.8064,6.018,6.0702,5.9472,5.6409,5.1517,4.4888,3.6699,2.7207,1.6732,0.5646,-0.5646,-1.6732,-2.7207,-3.6699,-4.4888,-5.1517,-5.6409,-5.9472,-6.0702,-6.018,-5.8064,-5.458,-5,-4.4632,-3.8794,-3.2798,-2.6929,-2.143,-1.6488,-1.2225,-0.8695,-0.5879,-0.3695,-0.2007,-0.0633,0.0633,0.2007,0.3695,0.5879,0.8695,1.2225,1.6488,2.143,2.6929,3.2798,3.8794,4.4632,5,5.458]",
encode = "json",
query = list(
amplitudeMulti = "1.0",
bartelsLimit = "49",
minCycleLength = "5",
maxCycleLength = "300",
sortByStrength = "true",
includeSpectrum = "false",
humanReadableText = "false",
api_Key = "aaaa"
)
)它比要求的b/c更冗长,它是以编程方式生成的。要将其放入您的原始代码中:
httr::POST(
url = "https://api.marketcycles.online/api/CycleScanner",
httr::accept_json()
encode = "json",
body = jsonlite::toJSON(val),
query = list(
amplitudeMulti = "1.0",
bartelsLimit = "49",
minCycleLength = "5",
maxCycleLength = "300",
sortByStrength = "true",
includeSpectrum = "false",
humanReadableText = "false",
api_Key = "aaaa"
)
)也许行得通。再一次,我只是去了Swagger,不能测试它。您还可以删除query位中的所有默认值。我喜欢在构建与API接口的包时传递它们。
发布于 2018-09-28 10:30:03
找到了解决方案。这是hrbrmstr建议的代码的一些调整。以下代码可以正常工作
httr::POST(
url = "https://api.marketcycles.online/api/CycleScanner",
httr::content_type_json(), # needed to add this
httr::accept_json(),
body =jsonlite::toJSON(val),
# encode = "json", # needed to delete this
query = list(
amplitudeMulti = "1.0",
bartelsLimit = "49",
minCycleLength = "5",
maxCycleLength = "300",
sortByStrength = "true",
includeSpectrum = "false",
humanReadableText = "false",
api_Key = "wtt****")
)谢谢你的帮助。
https://stackoverflow.com/questions/52510792
复制相似问题