4

我正在尝试使用 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
4

1 回答 1

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()
)

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

于 2014-04-07T21:45:33.040 回答