簡體   English   中英

RESTAssured 多部分內容類型

[英]RESTAssured Multipart content-type

我最近開始使用 RESTAssured,並且正在使用 RESTAssured 庫進行 REST 調用。

我在使用"multipart()"方法附加的請求中有一個附件。 對於我的 API,我應該將"application/x-abc-xyz+xml"作為內容類型傳遞。

當我嘗試使用" contentType()"設置它時,我得到以下錯誤,但是在內容類型前加上"multipart/"將解決此錯誤,但我沒有從服務器獲得 REST 響應,因為它期望內容類型沒有"multipart/"前綴。

我需要幫助來解決這個問題。

java.lang.IllegalArgumentException: Content-Type application/x-hub-multipart+xml is not valid when using multiparts, it must start with "multipart/". 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at io.restassured.internal.RequestSpecificationImpl.registerRestAssuredEncoders

它可能有效,你可以試試這個,例如:附件文件類型為“.png”

Response response = given()
                                   .multiPart(new MultiPartSpecBuilder(resourceFile).fileName(filename)
                                                                                    // controlName is the name of the
                                                                                    // RequestParam associated with the
                                                                                    // MultipartFile[] array
                                                                                    .controlName("file")
                                                                                    .mimeType("image/png")
                                                                                    .build())
                                   .param("documentType", "MyCat")  // You can omit this if U want
                                   .when()
                                   .post("my URI")
                                   .then()
                                   .extract()
                                   .response();

您可以傳遞內容類型,如:

.header("Content-Type", "multipart/json")

// 添加jira API 的附件示例。 確保啟用附件

given().log().all().header("X-Atlassian-Token","no-check").filter(session)
    .pathParam("Key", "10004")
    .header("Content-Type","multipart/form-data")
    .multiPart("file",new File("FilePath"))

    .when().post("/rest/api/2/issue/{Key}/attachments").then().log().all()
    .assertThat().statusCode(200);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM