Fixing Moco API POST JSON Parameter Extraction with a Custom JsonExtractor

When using Moco API for interface virtualization, the default extractor cannot handle POST requests with JSON bodies, so a custom JsonExtractor is created to read the request content, parse the JSON, and return the desired parameter value.

FunTester
FunTester
FunTester
Fixing Moco API POST JSON Parameter Extraction with a Custom JsonExtractor

During API virtualization with Moco, the example provided in the official documentation only supports extracting query parameters, which means a POST request that sends JSON data cannot be processed correctly.

The original ParamRequestExtractor class extends HttpRequestExtractor<String[]> and retrieves values from request.getQueries(), which works for URL‑encoded query strings but not for JSON payloads.

To solve this, a new JsonExtractor class is implemented. It reads the request content via request.getContent().toString(), parses the string into a JSONObject, extracts the value associated with the specified key, and returns it as a String[]. If parsing fails, an empty string array is returned.

public class JsonExtractor extends HttpRequestExtractor<String[]> {
    private final String param;
    public JsonExtractor(final String param) { this.param = param; }
    @Override
    protected Optional<String[]> doExtract(HttpRequest request) {
        try {
            String s = request.getContent().toString();
            String value = JSONObject.fromObject(s).getString(param);
            return fromNullable(new String[]{value});
        } catch (Exception e) {
            return fromNullable(new String[]{""});
        }
    }
    public static RequestExtractor<String[]> queryJson(final String param) {
        return new JsonExtractor(checkNotNullOrEmpty(param, "参数不能为空!"));
    }
}

In Groovy scripts, two helper methods are added:

/** get request parameter equality for GET */
static RequestMatcher eqArgs(String key, String value) {
    eq query(key), value
}
/** post request JSON parameter equality */
static RequestMatcher eqParams(String key, String value) {
    eq queryJson(key), value
}

These methods allow test definitions to compare query parameters ( eqArgs) and JSON body parameters ( eqParams) directly.

Groovy, being a JVM‑based dynamic language, offers strong Java compatibility—most Java files can be renamed to .groovy and compiled without changes, and vice‑versa. This compatibility reduces learning costs and enables seamless use of Java libraries. Modern IDEs such as IntelliJ provide robust Groovy support, and build tools like Gradle (which is Groovy‑based) often feel more convenient than Maven.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavatestingJSONGroovyMoCoAPI mockingHttpRequestExtractor
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.