'Unable to download Azure DevOps test result attachments using REST API in Groovy Unable to determine the current character

I need to download test result file attachments to a folder on my hard drive.

When I run the call using talend it returns the file (png) image file in the body

The GET call I'm sending is: https://dev.azure.com/myorg/Sandbox/_apis/test/Runs/1347512/Results/100001/attachments/1520956?api-version=6.0-preview.1

Returns the image in the body

enter image description here

When I run this call in Groovy I use the following method in the main class to call the testManagementService class

    def project = 'myproject'
    def runId = '1514421'
    def resultId = '100001'
    def attachId = '1520956'
    def downloadFiles = testManagementService.downloadResAttachments(project, runId, resultId, attachId)
    

Here's the downloadResAttachments method in the testManagementService class

public def downloadResAttachments(project, runId, resultId, attachId) {
    def collection = "zionseto"
    def eproject = URLEncoder.encode(project, 'utf-8')
    eproject = eproject.replace('+', '%20')
    
    def result = genericRestClient.get(
        contentType: ContentType.JSON,
        //requestContentType: ContentType.JSON,
        uri: "${genericRestClient.getTfsUrl()}/${collection}/${project}/_apis/test/Runs/${runId}/Results/${resultId}/attachments/${attachId}?",
        
        headers: ['Content-Type': 'application/json'],
        query: ['api-version':'6.0-preview.1', 'expand': 'all']
        )

    return result;

}

When I run this code it returns the following error:

Caused by: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object The current character read is '?' with an int value of 65533 Unable to determine the current character, it is not a string, number, array, or object line number 1 index number 0 ?PNG ^ at org.apache.groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:202) at org.apache.groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:153) at org.apache.groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:43) at org.apache.groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:380) at org.apache.groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:133) at groovy.json.JsonSlurper.parse(JsonSlurper.java:218) at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1260) at groovy.lang.MetaClassImpl.invokeMethodClosure(MetaClassImpl.java:1040) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1134) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1027) at groovy.lang.Closure.call(Closure.java:412) at groovy.lang.Closure.call(Closure.java:428) at groovyx.net.http.HTTPBuilder.parseResponse(HTTPBuilder.java:560) at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:489)

I'm guessing I need to modify the code to handle the file attachment download and save the file to disk?

Any help would be appreciated.



Solution 1:[1]

In the testManagementService I had to change Content-Type to 'application/octet-stream'

    def result = genericRestClient.get(
    **contentType: 'application/octet-stream',**
    uri: "${genericRestClient.getTfsUrl()}/${collection}/${project}/_apis/test/Runs/${runId}/Results/${resultId}/attachments/${attachId}?",
    
    headers: ['Content-Type': 'application/octet-stream'],
    query: ['api-version':'6.0-preview.1', 'expand': 'all']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mangel