无法使用Groovy中的REST API下载Azure DevOps测试结果附件无法确定当前字符

发布于 2025-01-26 16:15:15 字数 3278 浏览 3 评论 0原文

我需要将测试结果文件附件下载到硬盘上的文件夹中。

当我使用TALEND运行呼叫时,它将返回正文中的文件(PNG)图像文件,

我发送的获取呼叫是: https://dev.azure.com/myorg/sandbox/_apis/test/runs/1347512/1347512/results/100001/100001/attachments/1520956?api-version=6.0-preview.1

“ 正文

我在Groovy中运行此调用时,我在主类中使用以下方法来调用TestManagementservice类,

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

以下是TestManagementservice类中的下载resattachments方法,

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;

}

当我运行此代码时,它返回以下错误:

< em>引起的是:groovy.json.jsonexception:无法确定当前字符,它不是字符串,数字,数组或对象 当前的角色读是“?” int值为65533 无法确定当前字符,不是字符串,数字,数组或对象 行号1 索引号0 ?png ^ atrg.apache.groovy.json.internal.jsonparserchararray.decodevalueinternal(jsonparserchararray.java:202) atrg.apache.groovy.json.internal.jsonparserchararray.decodevalue(jsonparserchararray.java:153) atrg.apache.groovy.json.internal.jsonparserchararray.decodefromchars(jsonparserchararray.java:43) atrg.apache.groovy.json.internal.jsonparserchararray.parse(jsonparserchararray.java:380) atrg.apache.groovy.json.internal.basejsonparser.parse(basejsonparser.java:133) 在groovy.json.jsonslurper.parse(jsonslurper.java:218) 在groovyx.net.http.parserregistry.parsejson(parserregistry.java:280) 在sun.reflect.nativemethodaccessorimpl.invoke0(天然方法) 在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) 在sun.reflect.delegatingmethodaccessorimpl.invoke(授权methodaccessorimpl.java:43) 在java.lang.reflect.method.invoke(method.java:498) 在org.codehaus.groovy.reflection.cachedmethod.invoke(cachedmethod.java:107) 在groovy.lang.metamethod.domethodinvoke(metamethod.java:323) 在groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1260) 在groovy.lang.metaclassimpl.invokemethodosuble(metaclassimpl.java:1040) 在groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1134) 在groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1027) 在groovy.lang.closus.call.call(closure.java:412) 在groovy.lang.closus.call(closure.java:428) 在grovyx.net.http.httpbuilder.parseresponse(httpbuilder.java:560) 在groovyx.net.http.httpbuilder $ 1.HandlerSponse(httpbuilder.java:489)

我猜我需要修改代码以处理文件附件下载并将文件保存到磁盘上?

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2025-02-02 16:15:15

在TestManagementservice中,我必须将内容类型更改为“应用/八位字节”

    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']

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']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文