【问题标题】:RCurl JSON data to JIRA REST add issueRCurl JSON 数据到 JIRA REST 添加问题
【发布时间】:2014-04-07 19:53:06
【问题描述】:

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

【问题讨论】:

    标签: r rcurl rjsonio


    【解决方案1】:

    您可能会发现使用 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 调用失败。

    【讨论】:

    • 非常感谢。这做到了!展望未来,我将在 RCurl 上使用 httr
    • 如何从返回的响应中提取信息。我尝试了 response
    • @codeBarer content(req) - 查看文档以了解选项(原始、文本、解析)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多