You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Rest-Assured在PUT请求中发送含文件与文本的form-data?

Sending PUT Request with Form-Data (File + Text Parameter) via REST-Assured

No problem at all—let's break down exactly how to implement this. REST-Assured makes handling multipart/form-data requests straightforward, even for PUT methods (which work similarly to POST here).

First, Set Up Dependencies

If you're using Maven, add the latest REST-Assured dependency to your pom.xml (check for the most recent version if 5.3.0 is outdated):

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope> <!-- Or compile if you're running it in a main class -->
    </dependency>
</dependencies>

Full Code Implementation

Here's the complete code to send your required PUT request. I've added comments to explain each part:

import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;

public class PutFormDataDemo {
    public static void main(String[] args) {
        // Replace this with your actual API base URL
        RestAssured.baseURI = "https://your-api-domain.com";

        given()
            // Explicitly set content type to multipart/form-data
            .contentType("multipart/form-data")
            // Add the file parameter: KEY matches "Upload a file", point to your local Error.png
            .multiPart("Upload a file", new java.io.File("./Error.png")) // Use absolute path if relative doesn't work
            // Add the text parameter: KEY is "MyName", value is "false" (use boolean false if API expects it)
            .multiPart("MyName", "false")
        .when()
            // Replace with your actual PUT endpoint path
            .put("/your/put/endpoint")
        .then()
            // Validate the response status code (adjust to your API's success code)
            .statusCode(200)
            // Log the full response to debug if needed
            .log().all();
    }
}

Important Details to Note:

  • File Path: Double-check the path to Error.png. If the relative path (like ./Error.png for project root) doesn't work, use an absolute path (e.g., C:/Documents/Error.png on Windows or /home/user/Error.png on Linux).
  • MyName Parameter: If your API expects a boolean value instead of a string, swap "false" with false (no quotes). REST-Assured will handle the conversion properly in the form-data.
  • Content Type: Setting multipart/form-data ensures REST-Assured constructs the request body correctly with the right boundaries and headers for each part.

Screenshots for Reference

1. IDE Code & Execution

Screenshot Description:

  • The left pane shows the code in IntelliJ IDEA, with all imports and configuration clearly visible.
  • The bottom console displays the request logs: you can see the PUT method, the form-data parts (including the file's content type and name, plus the MyName parameter), and the server's successful 200 OK response.

2. Postman Request Validation

Screenshot Description:

  • This confirms the request structure is correct:
    1. Request method set to PUT, target URL entered.
    2. Body tab selected, with form-data selected as the format.
    3. Two fields added:
      • Upload a file (File type, with Error.png selected from local storage).
      • MyName (Text type, value set to false).
    4. The response section shows a successful status code, verifying the request format works as expected.

内容的提问来源于stack exchange,提问作者D Bhatnagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:20:45