Why HttpClient Skips Null Parameters and How to Fix It in Java
When using HttpClient for API testing, a JSONObject omits fields with null values, causing version parameters to be missing; initializing version strings to empty strings resolves the issue and ensures proper request payloads.
I was testing an API with HttpClient and encountered an endpoint that returns system resources, each tagged with a version. The intended logic is: on the first request, send an empty version to get the latest data; on subsequent requests, send the stored version to check for updates. I wrote a method that saves the versions from the first response and reuses them on later calls.
During testing I discovered that the first request, where I passed null for the version fields, did not actually include those parameters in the JSON payload. Investigation revealed that JSONObject.put(key, null) silently removes the key, so the parameter is omitted from the request.
/**
* Get static system resources
*
* First call stores each version, subsequent calls reuse them
*/
public JSONObject getResource() {
JSONObject response = null;
String url = urls.getString("static_config");
JSONObject args = getParams(token);
args.put("audio_country", "");
args.put("audio_label", "");
args.put("game_user_number", "");
args.put("banner", "");
args.put("video_open_time", "");
args.put("tip", "");
args.put("objective", objectiveVersion);
args.put("startup_page", startupVersion);
args.put("game", gameVersion);
args.put("app", appVersion);
args.put("gift", giftVersion);
args.put("app_media_config", videoVersion);
HttpGet httpGet = getHttpGet(url, args);
output(args.size());
response = getHttpResponseEntityByJson(httpGet);
if (response.containsKey("dataInfo") && objectiveVersion.isEmpty()) {
JSONObject dataInfo = response.getJSONObject("dataInfo");
objectiveVersion = dataInfo.getJSONObject("objective").getString("version");
startupVersion = dataInfo.getJSONObject("startup_page").getString("version");
gameVersion = dataInfo.getJSONObject("game").getString("version");
appVersion = dataInfo.getJSONObject("app").getString("version");
giftVersion = dataInfo.getJSONObject("gift").getString("version");
videoVersion = dataInfo.getJSONObject("video_open_time").getString("version");
}
return response;
}The problem stemmed from the version fields being declared as String members with default value null. When null is passed to JSONObject.put, the key is dropped, so the server receives no version information and cannot return the expected data.
public String objectiveVersion = ""; // purpose page version
public String startupVersion = ""; // startup page version
public String gameVersion = ""; // game version
public String appVersion = ""; // app update config version
public String giftVersion = ""; // gift version
public String videoVersion = ""; // video versionChanging the initialization from null to an empty string ( "") ensures that the keys are present in the JSON payload, even if their values are empty, allowing the API to process the request correctly and return the required version data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
