RCURl JSON数据到JIRA REST添加问题

时间:2014-04-07 19:53:06

标签: r rcurl rjsonio

我正在尝试使用R将数据发布到JIRA项目,我不断得到:错误请求错误。起初我认为它必须是我创建的JSON格式。所以我将JSON写入文件并从控制台执行curl命令(见下文)并且POST工作正常。

curl -D- -u fred:fred -X POST -d @sample.json -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/

这会将问题带到我的R代码中。有人能告诉我我在RCurl postForm上做错了什么吗?

来源:

library(RJSONIO)
library(RCurl)

x <- list (
  fields = list(
  project = c(
     c(key="TEST")
  ),
  summary="The quick brown fox jumped over the lazy dog",
  description = "silly old billy",
  issuetype = c(name="Task")
 )
)


curl.opts <- list(
  userpwd = "fred:fred",
  verbose = TRUE,
  httpheader = c('Content-Type' = 'application/json',Accept = 'application/json'),
  useragent = "RCurl"           
)

postForm("http://jirahost:8080/jira/rest/api/2/issue/",
     .params= c(data=toJSON(x)),
     .opts = curl.opts,
     style="POST"
)

rm(list=ls()) 
gc()

以下是回复的输出:

* About to connect() to jirahost port 80 (#0)
* Trying 10.102.42.58... * connected
* Connected to jirahost (10.102.42.58) port 80 (#0)
> POST /jira/rest/api/2/issue/ HTTP/1.1
User-Agent: RCurl
Host: jirahost
Content-Type: application/json
Accept: application/json
Content-Length: 337

< HTTP/1.1 400 Bad Request
< Date: Mon, 07 Apr 2014 19:44:08 GMT
< Server: Apache-Coyote/1.1
< X-AREQUESTID: 764x1525x1
< X-AUSERNAME: anonymous
< Cache-Control: no-cache, no-store, no-transform
< Content-Type: application/json;charset=UTF-8
< Set-Cookie: atlassian.xsrf.token=B2LW-L6Q7-15BO- MTQ3|bcf6e0a9786f879a7b8df47c8b41a916ab51da0a|lout; Path=/jira
< Connection: close
< Transfer-Encoding: chunked
< 
* Closing connection #0
Error: Bad Request

1 个答案:

答案 0 :(得分:4)

您可能会发现使用已构建的httr更容易 考虑到现代API的需求,并倾向于设置更好的默认值 选项。等效的httr代码是:

library(httr)

x <- list(
  fields = list(
    project = c(key = "TEST"),
    summary = "The quick brown fox jumped over the lazy dog",
    description = "silly old billy",
    issuetype = c(name = "Task")
  )
)

POST("http://jirahost:8080/jira/rest/api/2/issue/",
  body = RJSONIO::toJSON(x),
  authenticate("fred", "fred", "basic"),
  add_headers("Content-Type" = "application/json"),
  verbose()
)

如果这不起作用,您需要提供成功的输出 控制台上的详细卷曲,以及R中的失败的httr调用。