2012-02-09 65 views
7

鑑於此api documentation,我將如何使用HTTPBuilder和Groovy來構建我的查詢?我已經嘗試過很多東西,但是我沒有把它做好。使用Artifactory的REST API部署jar文件

def http = new HTTPBuilder() 
http.request('http://artifactory:8888/libs-snapshot-local/my/jar/1.0/test-jar-1.0.jar', PUT, JSON) { req -> 

     body = [ 
      uri: "http://artifactory:8888/libs-snapshot-local/my/jar/1.0/test-jar-1.0.jar", 
      downloadUri: "http://artifactory:8888/libs-snapshot-local/my/jar/1.0/test-jar-1.0.jar", 
      repo: "libs-snapshot-local", 
      path: "c:\\pathtojarfile\\test.jar", 
      created: "2012-02-03T08:37:12.599-0800", 
      createdBy: "someuser", 
      size: "1024", 
      mimeType: "application/java-archive" 

     ] 

    response.success = { resp, json -> 


    } 

    } 

這似乎讓我有部分路,但它上傳了一個空的jar文件。好像身體完全被忽略了一樣。刪除它會產生相同的結果。我似乎無法找到一個很好的參考如何做到這一點。

回答

13

上述文檔中的JSON實際上是Artifactory的響應到部署請求。
對於部署,Artifactroy只需要一個簡單的PUT請求,例如:

def restClient = new RESTClient('http://localhost:8080/artifactory/libs-release-local/') 
restClient.auth.basic 'username', 'password' 
restClient.encoder.'application/zip' = this.&encodeZipFile 
def encodeZipFile(Object data) throws UnsupportedEncodingException { 
    def entity = new FileEntity((File) data, 'application/zip'); 
    entity.setContentType('application/zip'); 
    return entity 
} 
def response = restClient.put(path: 'org/artifact/1.0/artifact-1.0.jar', 
     body: new File('/path/to/local/artifact.jar'), 
     requestContentType: 'application/zip' 
) 
+0

這個工作!謝謝!該文件有點令人困惑。其中一些人說「樣本輸出」,一些人說「樣本用法」。部署的API有「示例用法」....我認爲這將是如何調用API。 – stuff22 2012-02-10 17:08:59