如何使用java在restassured中上载文件

如何使用java在restassured中上载文件,java,rest-assured,Java,Rest Assured,我们有一个API,它从系统中获取一个文件并在应用程序上显示,我正试图用rest assured和Java实现自动化 我已经尝试过了,将图像更改为二进制代码,然后将其添加为不起作用的参数 Map<String, String> paramSample = new HashMap<>(); paramSample.put("api_key", "A813302*************"); paramSample.put("method", "my");

我们有一个API,它从系统中获取一个文件并在应用程序上显示,我正试图用rest assured和Java实现自动化

我已经尝试过了,将图像更改为二进制代码,然后将其添加为不起作用的参数

Map<String, String> paramSample = new HashMap<>();
    paramSample.put("api_key", "A813302*************");
    paramSample.put("method", "my");
    paramSample.put("body", "{\n" +
            "  \"to\":\"91xxxxxxxx\",\n" +
            " \"type\": \"image\", \"image\" : {\"caption\" : \"{{caption}}\"},\n" +
            "\"callback\":\"{{callback}}\"\n" +
            "}");
    paramSample.put("from", "91xxxxxxx");
    paramSample.put("file","C:\\Users\\sobhit.s\\Pictures\\SMS-2047.png");
    RequestSpecification request = given();
    Response responseSample = request.params(paramSample).get(ExecutionConfig.BASE_URL).then().extract().response();
    String res=responseSample.prettyPrint();

首先,如果您不确定,请在Postman中执行此操作,然后在代码中重新创建相同的操作。 这样,您将有一个邮差来演示您的编码问题

仅将.queryParam用于参数,而不用于正文内容。正文内容应在.Body下

使用.multiPart将文件作为多部分任务上载。希望这有帮助

given().queryParam(
            "api_key", "A813302*************", 
            "method", "my",
            "from", "91xxxxxxx")
            .body("{\n" +
            "  \"to\":\"91xxxxxxxx\",\n" +
            " \"type\": \"image\", \"image\" : {\"caption\" : \"{{caption}}\"},\n" +
            "\"callback\":\"{{callback}}\"\n" +
            "}")
            .multiPart(new File("C:/Users/sobhit.s/Pictures/SMS-2047.png"))
            .when()
            .get(ExecutionConfig.BASE_URL)
            .prettyPrint();

使用gived.paramsparamSample.multiPartfile以多部分请求的形式发送文件。@SudhirR您能详细说明一下吗?您是天才。上个月我一直在讨论这个问题。还有一个问题可以把它当作某种函数,这样值就会传入函数,然后它就会执行,这样主代码就会被隐藏。这将是一种抽象。。。让我知道你的想法。
given().queryParam(
            "api_key", "A813302*************", 
            "method", "my",
            "from", "91xxxxxxx")
            .body("{\n" +
            "  \"to\":\"91xxxxxxxx\",\n" +
            " \"type\": \"image\", \"image\" : {\"caption\" : \"{{caption}}\"},\n" +
            "\"callback\":\"{{callback}}\"\n" +
            "}")
            .multiPart(new File("C:/Users/sobhit.s/Pictures/SMS-2047.png"))
            .when()
            .get(ExecutionConfig.BASE_URL)
            .prettyPrint();