RESTAssured Multipart内容类型

时间:2017-10-02 18:06:24

标签: rest cucumber rest-assured gherkin cucumber-java

我最近开始使用RESTAssured。我正在使用RESTAssured库进行REST调用。我在请求中有一个附件,我使用 #!/usr/bin/env python3 # imports import requests import time from tkinter import * import random # variables test = 'https://api.nicehash.com/api?method=stats.provider.ex&addr=37sCnRwMW7w8V7Y4zyVZD5uCmc9N1kZ2Q8&callback=jQuery111304118770088094035_1506738346881&_=1506738346882' url = 'https://api.coinbase.com/v2/prices/USD/spot?' # def function to update def update_bitcoin_ui(): # update the data sourced form the website req = requests.get(url) data = req.json() bit = (data['data'][0]['amount']) # update the gui to reflect new value thelabel.config(text = "1 BTC = %s USD" % bit) # verify the Ui is updating #thelabel.config(text=str(random.random())) root.after(1000, update_bitcoin_ui) # gui workspace root = Tk() thelabel = Label(root, text = "") # set more of the gui and launch the ui thelabel.pack() root.after(1000, update_bitcoin_ui) root.mainloop() 方法附加。对于我的API,我应该将 "multipart()" 作为Content-Type传递。当我尝试使用 "application/x-abc-xyz+xml" 方法进行设置时,我得到以下错误。但是在内容类型之前加上" multipart /"将解决此错误但我没有从服务器获得REST响应,因为它期望没有 " contentType()" 前缀的内容类型。我需要帮助来解决这个问题。任何帮助,将不胜感激。谢谢!

  

java.lang.IllegalArgumentException: Content-Type   application / x-hub-multipart + xml在使用multiparts时无效   必须以" multipart /"。开头   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)   在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  在   org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)   在   org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)   在   org.codehaus.groovy.runtime.callsite.ConstructorSite $ ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)   在   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)   在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)   在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)   在   io.restassured.internal.RequestSpecificationImpl.registerRestAssuredEncoders

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

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

答案 2 :(得分: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);