HTTR包POST命令不从REST API返回json

时间:2015-07-11 14:31:07

标签: json r api rest

我在将R连接到应用程序的REST API时遇到问题。

我可以使用RCurl包的getURL函数从REST API调用命令,如下所示,它可以很好地工作。

<modelVersion>4.0.0</modelVersion>
    <groupId>simple</groupId>
    <artifactId>simple</artifactId>
    <version>1.0-SNAPSHOT</version>
     <packaging>war</packaging>

然后可以使用fromJSON(RESTResult)解析此RESTResult,它可以很好地工作。

但是,我找不到发送包含头的POST RCurl请求的等效方法,所以我转移到httr包发送POST请求。以下代码有效,但fromJSON函数返回错误:

RESTQuery = paste0(MyBaseURL,MyRESTQuery)
RESTHeader = c(
    paste0("Host:",MyServerIP,":",MyServerPort),
    paste0("Authorization:Basic ",MyEncodedToken),
    "Connection: keep-alive"
)
RESTResult = getURL(URL=RESTQuery,httpheader=RESTHeader,verbose=true)

返回的值是从REST API正确返回的,但它们似乎没有格式化为JSON,因此我无法将它们解析为一个漂亮的数据帧。以下是以详细模式查询的返回代码:

RESTQuery = paste0(MyBaseURL,MyRESTQuery)
RESTPOSTHeader = c(
    'Content-Type'="application/json; charset=utf-8",
    'Authorization'=paste0("Basic ",MyEncodedToken),
    'Accept'="application/json"
)
RESTPOSTBody = c(
    "$expand=Axes,Cells"
)
RESTResult <- POST(RESTQuery,query=RESTPOSTBody,add_headers(RESTPOSTHeader),verbose())

我使用内容时的值(RESTResult):

-> User-Agent: curl/7.39.0 Rcurl/1.95.4.5 httr/0.6.1
-> Host: <My server>:<My Port>
-> Accept-Encoding: gzip
-> Cookie: SessionId=NMD-MavwPL_QhdHFMRyhZQ
-> Content-Type: application/json; charset=utf-8
-> Authorization: Basic <My Encoded Token>
-> Accept: application/json
-> Content-Length: 0
-> 
<- HTTP/1.1 201 Created
<- Content-Length: 944
<- Connection: keep-alive
<- Content-Encoding: gzip
<- Cache: no-cache
<- Content-Type: application/json;odata.metadata=minimal;odata.streaming=true; charset=utf-8
<- Location: ../../Cellsets('sIYzNM4HAIAGAAAg')
<- OData-Version: 4.0
<- Set-Cookie: SessionId=7V5XAXXjwfbaXmdOb3FgJQ; Path=/api/; HttpOnly

当我尝试使用fromJSON格式化时,我得到错误“Argument'txt'必须是JSON字符串,URL或文件”,如果我尝试以下任何一种:

  • fromJSON(RESTResult)
  • fromJSON(含量(RESTResult))
  • fromJSON(含量(RESTResult)$细胞)

所以,我有点卡在以下方面:

  1. 如何使用RCurl包发送POST请求 或
  2. 如何使用HTTR包POST命令从REST API中检索正确的JSON结果?
  3. 我设置标题是错误的,还是做了一些非常愚蠢的事情?任何建议都会很棒! 谢谢!

0 个答案:

没有答案