RESTAssured多部分内容类型

3
我最近开始使用RESTAssured,并使用RESTAssured库进行REST调用。
我有一个请求中的附件,我使用"multipart()"方法附加。对于我的API,应该传递"application/x-abc-xyz+xml"作为Content-Type。
当我尝试使用"contentType()"设置此内容时,会出现以下错误,但在Content-Type之前添加"multipart/"将解决此错误,但是我无法从服务器获取REST响应,因为它期望不带"multipart/"前缀的content-type。
我需要帮助解决这个问题。
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
3个回答

2

这可能有效,你可以尝试一下,例如:将附件文件类型设置为“.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();

谢谢您的回复。我已经尝试过了。您在代码中没有传递任何内容类型,因此它将采用“multipart/form-data”作为默认内容类型。在我的情况下,我正在尝试传递“application/x-abc-xyz+xml”作为内容类型。 - Coder12345

1
您可以像这样传递内容类型:

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

1
谢谢您,先生!在我的情况下,缺少“multipart/form-data”头,而我的控制器接受它。 - Dmitriy

0

// 添加 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接