简体   繁体   English

RESTAssured 多部分内容类型

[英]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.

相关问题 Restassured 测试错误 - No Content-Type was specified in response - Restassured test error - No Content-Type was specified in response 服务无法识别RestAssured内容类型application / json - RestAssured content-type application/json not being recognised by service 使用curl rest api发送,具有多个内容类型的多部分数据 - send with curl rest api, multipart data with multiple content-type 大吃一惊,如何在多部分/表单数据中强制使用内容类型 - guzzle, how to force content-type in a multipart/form-data REST和内容类型 - REST and content-type 内容类型定界符 - Content-Type delimiter 对于多部分数据请求,应在Content-Type标头中使用哪个分隔符?逗号或分号? - Which separator should be used in the Content-Type header for a multipart data request? Comma or Semicolon? 如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型 - How to set content-type for the file in multipart upload when using RestTemplate (from a rest client) 有什么办法可以通过附件的内容类型来过滤请求(multipart / form-data)? - Is there any way to filter request (multipart/form-data) by content-type of attached file? 使用Content-Type进行发布:multipart / form-data导致空的请求正文 - POSTing with Content-Type: multipart/form-data results in empty request body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM