[英]RESTAssured Multipart content-type
I started using RESTAssured recently and am making a REST call using the RESTAssured library.我最近开始使用 RESTAssured,并且正在使用 RESTAssured 库进行 REST 调用。
I have an attachment in the request which I am attaching using the "multipart()"
method.我在使用
"multipart()"
方法附加的请求中有一个附件。 For my API I should be passing "application/x-abc-xyz+xml"
as the Content-Type.对于我的 API,我应该将
"application/x-abc-xyz+xml"
作为内容类型传递。
When I tried setting this using " contentType()"
I get below error, however preceding the content-type with "multipart/"
will resolve this error but I am not getting the REST response from the server because it expects the content-type without the "multipart/"
prefix.当我尝试使用
" contentType()"
设置它时,我得到以下错误,但是在内容类型前加上"multipart/"
将解决此错误,但我没有从服务器获得 REST 响应,因为它期望内容类型没有"multipart/"
前缀。
I need help in resolving this issue.我需要帮助来解决这个问题。
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
it may work, you can try this , Eg: Attachment file type as ".png"它可能有效,你可以试试这个,例如:附件文件类型为“.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")
// add attachments example of jira API. // 添加jira API 的附件示例。 Make sure to enable attachement
确保启用附件
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.