Mastering POST API Testing with FunTester: A Hands‑On Java Demo
This article walks through implementing and testing a POST API using the FunTester framework, covering a complete Java demo, handling parameter dependencies, authentication flow, and practical tips for short‑term test projects, with a full code example and a Gitee repository link.
Overview
After covering GET requests, this article demonstrates how to test POST interfaces using the FunTester framework. It shows a simple POST request demo, discusses difficulties encountered during recording, and explains why a single‑shot approach works only for simple APIs.
The second part illustrates handling validation values and parameter dependencies in test scenarios, providing a lightweight demo suitable for short‑term testing projects that can be written, executed, and discarded quickly.
Repository
Gitee repository: https://gitee.com/fanapi/tester
Code Example
package com.fun;
import com.alibaba.fastjson.JSONObject;
import com.fun.frame.httpclient.FanLibrary;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
public class AR extends FanLibrary {
public static String APIKEY;
public static void main(String[] args) {
// developerLogin();
registerUser();
testOver();
}
public static void registerUser() {
if (StringUtils.isEmpty(APIKEY)) developerLogin();
String url = "https://api.apiopen.top/registerUser";
JSONObject param = new JSONObject();
param.put("apikey", APIKEY);
param.put("name", "FunTester0021");
param.put("passwd", "123456");
param.put("nikeName", "FunTester");
param.put("headerImg", "http://pic.automancloud.com/sick-jvm-heap-1.png");
param.put("phone", "13100001111");
param.put("email", "[email protected]");
param.put("vipGrade", "3");
param.put("autograph", "abc");
param.put("remarks", "This is a test user!");
HttpPost httpPost = getHttpPost(url, param);
JSONObject response = getHttpResponse(httpPost);
output(response);
}
public static void register() {
String url = "https://api.apiopen.top/developerRegister";
JSONObject param = new JSONObject();
param.put("name", "FunTester");
param.put("passwd", "FunTester");
param.put("email", "[email protected]");
HttpPost httpPost = getHttpPost(url, param);
JSONObject response = getHttpResponse(httpPost);
output(response);
}
public static void developerLogin() {
String url = "https://api.apiopen.top/developerLogin";
JSONObject params = new JSONObject();
params.put("name", "funtester");
params.put("passwd", "funtester");
HttpPost httpPost = getHttpPost(url, params);
JSONObject response = getHttpResponse(httpPost);
output(response);
if (response.getIntValue("code") == 200) {
APIKEY = response.getJSONObject("result").getString("apikey");
} else {
fail();
}
}
}Key Points
POST requests require constructing a JSON payload and sending it via HttpPost.
When the API requires an API key, obtain it first via a login request; the example stores the key in a static variable.
Parameter dependencies (e.g., API key required before other calls) are handled by checking the variable and invoking developerLogin() if needed.
The demo uses the FunTester base class FanLibrary which provides helper methods getHttpPost, getHttpResponse, and output.
This approach is suitable for quick validation of simple POST endpoints but may not scale to complex business logic where multi‑step workflows are required.
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.
