2
votes

I’m trying to search artifacts using a groovy script and the REST API:

def query = ‘artifactory/api/search/artifact?name=at.mic.all.feature&repos=features-qa-test-snapshot’
def server = new RESTClient(‘http://mic-build2.mic.co.at:8081’)
server.parser.'application/vnd.org.jfrog.artifactory.search.ArtifactSearchResult+json' = server.parser.'application/json'
def resp = server.get(path: query)                       

this causes an exception:

groovyx.net.http.HttpResponseException: Bad Request
    at groovyx.net.http.RESTClient.defaultFailureHandler(RESTClient.java:240)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
    at groovyx.net.http.RESTClient.get(RESTClient.java:118)
    at groovyx.net.http.RESTClient$get.call(Unknown Source)
    at Artifactory.getArtifacts(artifactory.groovy:157)
…

Only to see if server and query are correct, I tried the url = server + ‘/’ + query (= http://mic-build2.mic.co.at:8081/artifactory/api/search/artifact?name=at.mic.all.feature&repos=features-qa-test-snapshot) in a browser, that results in a file that contains exactly my search result:

{"results":[{"uri":"http://mic-build2.mic.co.at:8081/artifactory/api/storage/features-qa-test-snapshot/at/mic/all/feature/at.mic.all.feature/20130313002236/at.mic.all.feature-20130313002236-win32.win32.x86.zip"},…..}

So, my server and query seem to be ok, but won’t work in my groovy script.

Thanks in advance for any help, kind regards! Frank

2

2 Answers

4
votes

The uri path should start with '/', HTTP client won't append it for you.

def query = ‘/artifactory/api/search/artifact?name=at.mic.all.feature&repos=features-qa-test-snapshot’

This should work.

4
votes

I had the similar problem with searching artifacts (it resulted with eigher "Bad Request" or "Not found" exceptions). The reason of the problem is that HTTP request created with your approach is malformed: in the artifactory request.log I see that the request

/api/search/artifact?name=test

turns into

/api/search/artifact%3Fname=test

which is obviously incorrect HTTP request. After investigation of the RESTClient documentation I found the right solution: form HTTP request using provided API instead of concatenating whole request as a string on your own.

def queryPath = '/api/search/artifact'
def server = new RESTClient('http://mic-build2.mic.co.at:8081')
server.parser.'application/vnd.org.jfrog.artifactory.search.ArtifactSearchResult+json' = server.parser.'application/json'
def resp = server.get(path: queryPath, query: [name: 'test', repos: 'your-repo'])

This way you can chain any parameters to the query without need to form HTTP request manually with proper separators. Hope this will help