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

使用REST Assured做Web服务测试:POST请求传参时返回失败响应

解决REST Assured POST请求中JSON参数导致的ID格式错误问题

Hey, let's break down why you're seeing that Malformed ID(s) detected error when using JSONObject or GSON, but hardcoding the JSON works perfectly. The error message Id: null format is not valid gives us a clear clue: one of your ID fields in the JSON payload is either missing or has a null value—something that's not happening with your hardcoded string.

Here are the steps to fix this:

1. Fix Your JSONObject Field Assignments

Looking at your code, all your json.put("", lp.getProperty("")) lines have empty keys and empty property names. That's definitely the problem! You need to replace those empty strings with the actual field names required by the API (like "userId", "orderId", whatever the correct ID field is) and the corresponding keys from your properties file.

For example, if the API expects an id field, your code should look like:

json.put("id", lp.getProperty("api.id")); // Replace "api.id" with the actual key in your properties file
// Add other fields the same way

2. Verify Your Properties File Loading

Make sure your lp.load() method is correctly loading the properties file, and that the keys you're using (like "api.id") actually exist in the file with a non-null, valid value. Add a quick debug print to confirm:

String idValue = lp.getProperty("api.id");
System.out.println("Loaded ID value: " + idValue); // Check if this is null or empty

3. Compare Generated JSON vs Hardcoded JSON

Before sending the request, print out the JSON string generated by JSONObject and compare it to your working hardcoded string. This will show you exactly where the mismatch is (e.g., missing fields, null values, incorrect field names):

System.out.println("Generated request body: " + json.toString());

If the generated JSON has "id": null but your hardcoded version has "id": "12345", you'll know the issue is with how you're loading the ID value from the properties file.

4. Try Alternative Payload Serialization Methods

If JSONObject is giving you trouble, REST Assured supports simpler ways to build request bodies that avoid manual JSON construction:

Option A: Use a HashMap

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("id", lp.getProperty("api.id"));
requestBody.put("otherField", lp.getProperty("other.field"));
// Add all required fields
request.body(requestBody); // REST Assured will auto-serialize this to JSON

Option B: Use a POJO (Plain Old Java Object)

Create a class that matches the API's request structure:

public class ApiRequest {
    private String id;
    private String otherField;
    // Add getters and setters for all fields
}

Then use it in your test:

ApiRequest payload = new ApiRequest();
payload.setId(lp.getProperty("api.id"));
payload.setOtherField(lp.getProperty("other.field"));
request.body(payload); // REST Assured handles serialization automatically

5. Double-Check Field Name Case Sensitivity

APIs often treat field names as case-sensitive. If your hardcoded string uses "ID" but you're putting "id" in the JSONObject, that could cause the API to not recognize the field, leading to a null value error. Make sure the field names exactly match what the API expects.

Once you fix the missing/misassigned ID field, your request should work just like the hardcoded version!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:49:33