我最近开始使用 RESTAssured,并且正在使用 RESTAssured 库进行 REST 调用。
我在使用 "multipart()"
方法附加的请求中有一个附件。对于我的 API,我应该传递 "application/x-abc-xyz+xml"
作为 Content-Type。
当我尝试使用 "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();
关于RESTAssured 多部分内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531172/