RCurl JSON data for JIRA REST add problem

I am trying to execute POST data in a JIRA Project using R, and I keep getting: Error Bad Request. At first, I thought it should be the JSON format I created. So I wrote JSON for writing and ran the curl command from the console (see below), and POST worked fine.

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

Which is causing the problem in my R code. Can someone tell me what I am doing wrong with RCFl postForm?

Source:

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

Here's the answer output:

* 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
source share
1 answer

It may be easier for you to use httrthat was created using the needs of modern APIs and, as a rule, improves the default options. The equivalent httr code would be:

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

, verbose curl httr R.

+4

Source: https://habr.com/ru/post/1535436/


All Articles