2014-04-07 1 views
4

R을 사용하여 JIRA 프로젝트에 POST 데이터를 보내려고하고 있습니다. 오류 요청이 계속 발생합니다. 처음에는 내가 만든 JSON 형식이어야한다고 생각했습니다. 그래서 JSON을 파일로 작성하고 콘솔 (아래 참조)에서 컬 명령을 실행했고 POST는 정상적으로 작동했습니다.RCurl JSON 데이터를 JIRA REST에 추가하십시오.

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

당신은 쉽게 으로 염두에두고 현대의 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() 
) 

, 당신은 콘솔에 성공적으로 자세한 컬의 출력을 제공해야하며,

+0

R.에 실패 HTTR 전화 : 등가 HTTR 코드는 것 정말 고맙습니다. 이것은 그것을했다! 앞으로 RCurl을 통해 httr을 사용할 것입니다. – codeBarer

+0

반환 된 응답에서 정보를 추출하려면 어떻게해야합니까? 나는 응답을 시도했다. POST (...) 응답을 $ 할 때 나는 16 진수로 값을 얻는다. – codeBarer

+0

@codeBarer'content (req)'- 옵션 (raw, text, parsed)에 대한 문서보기 – hadley

관련 문제