简体   繁体   中英

RESTAssured Multipart content-type

I started using RESTAssured recently and am making a REST call using the RESTAssured library.

I have an attachment in the request which I am attaching using the "multipart()" method. For my API I should be passing "application/x-abc-xyz+xml" as the Content-Type.

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.

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"

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM