Mastering FanLibrary HTTP Requests: Get, Post, and File Upload Techniques

This article explains how the FanLibrary testing framework provides overloaded methods to create HttpGet and HttpPost objects, supporting JSON, form, and multipart file uploads, with detailed Java code examples and guidance on parameter handling and URL encoding.

FunTester
FunTester
FunTester
Mastering FanLibrary HTTP Requests: Get, Post, and File Upload Techniques

GET request utilities

The FanLibrary class provides overloaded getHttpGet methods to create HttpGet objects.

Signature:

public static HttpGet getHttpGet(String url, JSONObject args)

If args is null or empty, the method delegates to the simple overload.

Otherwise it calls changeJsonToArguments(args) to convert the JSON key/value pairs into a URL‑encoded query string, concatenates it to url, removes any spaces, and returns a new HttpGet instance.

Simple overload: public static HttpGet getHttpGet(String url) returns new HttpGet(url).

/**
 * Build a GET request with optional JSON query parameters.
 * @param url  Base request URL
 * @param args JSON object whose entries become URL‑encoded query parameters
 * @return HttpGet ready for execution
 */
public static HttpGet getHttpGet(String url, JSONObject args) {
    if (args == null || args.size() == 0) return getHttpGet(url);
    String uri = url + changeJsonToArguments(args); // e.g. "?key1=value1&key2=value2"
    return getHttpGet(uri.replace(" ", ""));
}

public static HttpGet getHttpGet(String url) {
    return new HttpGet(url);
}

POST request utilities

Several overloaded getHttpPost methods cover the most common payload types.

Form‑encoded parameters – accepts a JSONObject, builds a UrlEncodedFormEntity via setFormHttpEntity, and adds the Content‑Type: application/x-www-form-urlencoded header.

Raw JSON string – creates a StringEntity with UTF‑8 charset, sets the Content‑Type: application/json header.

Combined URL query (JSON) and body (JSON string) – concatenates the query string to the URL and forwards to the JSON‑string overload.

Multipart file upload – accepts a File together with other form fields, builds a multipart entity where the field named "file" is treated as binary data.

/** POST with form parameters */
public static HttpPost getHttpPost(String url, JSONObject params) {
    HttpPost httpPost = getHttpPost(url);
    setFormHttpEntity(httpPost, params);
    httpPost.addHeader(HttpClientConstant.ContentType_FORM);
    return httpPost;
}

/** POST with raw JSON string */
public static HttpPost getHttpPost(String url, String json) {
    HttpPost httpPost = getHttpPost(url);
    httpPost.setEntity(new StringEntity(json, DEFAULT_CHARSET.toString()));
    httpPost.addHeader(HttpClientConstant.ContentType_JSON);
    return httpPost;
}

/** Multipart POST with file upload */
public static HttpPost getHttpPost(String url, JSONObject params, File file) {
    HttpPost httpPost = getHttpPost(url);
    setMultipartEntityEntity(httpPost, params, file);
    return httpPost;
}

Multipart entity construction

The helper setMultipartEntityEntity creates a MultipartEntityBuilder, iterates over the JSON parameters, and adds each entry as either a binary part (when the value equals "file") or a text part.

private static void setMultipartEntityEntity(HttpPost httpPost, JSONObject params, File file) {
    String fileName = file.getName();
    InputStream inputStream = null;
    try { inputStream = new FileInputStream(file); }
    catch (FileNotFoundException e) { logger.warn("File not found", e); }

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    for (String key : params.keySet()) {
        String value = params.getString(key);
        if ("file".equals(value)) {
            builder.addBinaryBody(key, inputStream,
                ContentType.create(HttpClientConstant.CONTENTTYPE_MULTIPART_FORM),
                fileName);
        } else {
            StringBody body = new StringBody(value,
                ContentType.create(HttpClientConstant.CONTENTTYPE_TEXT, DEFAULT_CHARSET));
            builder.addPart(key, body);
        }
    }
    HttpEntity entity = builder.build();
    httpPost.setEntity(entity);
}

These utilities abstract away URL encoding, header management, and multipart construction, allowing test code to obtain ready‑to‑use HttpGet or HttpPost objects with minimal boilerplate.

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.

JavaBackend Developmentfile uploadHTTPTesting framework
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.