2014-06-11 8 views
0

안녕하세요 저는 grails에서 POST를하려고하는데 2 가지 방법을 시도했습니다. 나는이 curl 명령을 복제 걸려 :grails의 POST가 작동하지 않습니다.

curl -F [email protected] http://server.com/service.pl > out.xml 

이 명령은 잘 작동합니다.

def actualInputFile = new File("/path/to/file/in.xml"); 

    def http = new HTTPBuilder('http://server.com/service.pl') 
    println "START POST" 
    def postBody = [upload:actualInputFile] // will be url-encoded 

    http.post(body: postBody, 
      requestContentType: XML) { resp -> 

     println "POST Success: ${resp.statusLine}" 
    } 

이 그냥 전혀 작동하지 않습니다

def actualInputFile = new File("/path/to/file/in.xml"); 


    def retval = [success: false, message: null]; 


     HttpClient postClient = new HttpClient(); 
     MultipartPostMethod postMethod = new MultipartPostMethod('http://server.com/service.pl'); 
     postClient.httpConnectionManager.getParams().setSoTimeout(300000); 

     postMethod.addParameter("upload",actualInputFile); 

     int status = postClient.executeMethod(postMethod); 
     def data = postMethod.getResponseBodyAsString(); 
     postMethod.releaseConnection(); 
     def returnedFile = new File("/path/to/file/out.xml"); 
      returnedFile.write(data); 

이 500 오류 나는 또한 시도

에게 제공 : 내가 지금까지 시도 무엇 는 다음과 같습니다.

500 개 용도를 얻는 첫번째

메소드 절하.

어떤 아이디어가 있습니까?

답변

0

은 내가 사용하지 않는 것이 좋습니다 그래서 경우, 컨트롤러에서이 코드를 실행하려고하는 가정 공유지를-HttpClient를이 작업하기 다루기 일반적으로 많은 의존성에 끌어한다. Grails를 함께 사용하기 위해 권장되는 클라이언트는 http://grails.org/plugin/rest-client-builder

입니다

귀하의 예는 다음과 같이 쓸 수있다 : 그럼에도 불구하고

def rest = new RestBuilder('http://server.com/service.pl') 
def resp = rest.post(url) { 
    upload = new File("/path/to/file/in.xml") 
} 
println resp.status 

당신이 사용해야하는 경우 실제로 실제 오류가 당신이 무엇 게시하는 경우 공유지를-HttpClient를 도움이 될 것입니다 봄. 단지 오류 메시지를 게시하지 않고 500을 얻는다는 것은 그렇게 도움이되지 않습니다.

0

이것은 다소 혼란 스럽습니다. Perl과 Curl에서 XML을 API에 POST하려고 시도하는 것처럼 보입니다. 그 맞습니까?

그래서 당신은 쉽게 그렇게 할 수 있지만 헤더를 지정하고 XML을 수용하고 해석하기 위해 API를 구축해야합니다.

그러나 오해의 소지가 있습니다.

관련 문제