如何使用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.pngfor project root) doesn't work, use an absolute path (e.g.,C:/Documents/Error.pngon Windows or/home/user/Error.pngon Linux). - MyName Parameter: If your API expects a boolean value instead of a string, swap
"false"withfalse(no quotes). REST-Assured will handle the conversion properly in the form-data. - Content Type: Setting
multipart/form-dataensures 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:
- Request method set to PUT, target URL entered.
- Body tab selected, with
form-dataselected as the format. - Two fields added:
Upload a file(File type, with Error.png selected from local storage).MyName(Text type, value set tofalse).
- The response section shows a successful status code, verifying the request format works as expected.
内容的提问来源于stack exchange,提问作者D Bhatnagar
相关产品推荐
相关产品推荐

