import ballerina/http;endpoint http:Listener chunkingEP {
    port:9092
};endpoint http:Listener echoEP {
    port:9090
};
endpoint http:Client clientEndpoint {
    targets: [
        {
           url: "http://localhost:9090"
        }
    ],
    chunking: http:CHUNKING_NEVER
};@http:ServiceConfig {
}
service<http:Service> chunkingSample bind chunkingEP {
    @http:ResourceConfig {
        path:"/"
    }
    sample (endpoint conn, http:Request req) {
        http:Request newReq = new;
        newReq.setJsonPayload({"hello":"world!"});
        var result = clientEndpoint -> post("/echo/", newReq);
        match result {
            http:Response clientResponse => {
                _ = conn -> respond(clientResponse);
            }
            http:HttpConnectorError err => {
                http:Response errorResponse = new;
                json errMsg = {"error":"error occurred while invoking the service"};
                errorResponse.setJsonPayload(errMsg);
                _ = conn -> respond(errorResponse);
            }
        }
    }
}
@http:ServiceConfig {
}
service<http:Service> echo bind echoEP {
    @http:ResourceConfig {
        path:"/"
    }
    echoResource (endpoint conn, http:Request req) {
        string value;
        if (req.hasHeader("content-length")) {
            value = "Lenght-" + req.getHeader("content-length");
        } else if (req.hasHeader("Transfer-Encoding")) {
            value = req.getHeader("Transfer-Encoding");
        } else {
            value = "Neither Transfer-Encoding nor content-length header found";
        }
        http:Response res = new;
        res.setJsonPayload({"Outbound request content":value});
        _ = conn -> respond(res);
    }
}

HTTP Disable Chunking

Example depicts the method of changing ballerina http client connector chunking option. By default http client sends content-length messages and if the message size is larger than the buffer size (8K) messages are chunked. But user can disable chunking using connector Options.

import ballerina/http;
endpoint http:Listener chunkingEP {
    port:9092
};
endpoint http:Listener echoEP {
    port:9090
};
endpoint http:Client clientEndpoint {
    targets: [
        {
           url: "http://localhost:9090"
        }
    ],
    chunking: http:CHUNKING_NEVER
};

Config client endpoint chunking behaviour by adding auto (default value), always or never to chunking option.

@http:ServiceConfig {
}
service<http:Service> chunkingSample bind chunkingEP {
    @http:ResourceConfig {
        path:"/"
    }
    sample (endpoint conn, http:Request req) {

Server does a backend call using chunking disabled HttpClient

        http:Request newReq = new;
        newReq.setJsonPayload({"hello":"world!"});
        var result = clientEndpoint -> post("/echo/", newReq);
        match result {
            http:Response clientResponse => {

Create new outbound request and set payload.

                _ = conn -> respond(clientResponse);
            }
            http:HttpConnectorError err => {
                http:Response errorResponse = new;
                json errMsg = {"error":"error occurred while invoking the service"};
                errorResponse.setJsonPayload(errMsg);
                _ = conn -> respond(errorResponse);
            }
        }
    }
}

Forward the inbound response.

@http:ServiceConfig {
}
service<http:Service> echo bind echoEP {
    @http:ResourceConfig {
        path:"/"
    }
    echoResource (endpoint conn, http:Request req) {
        string value;

Sample backend which respond according chunking behaviour.

        if (req.hasHeader("content-length")) {
            value = "Lenght-" + req.getHeader("content-length");
        } else if (req.hasHeader("Transfer-Encoding")) {
            value = req.getHeader("Transfer-Encoding");
        } else {
            value = "Neither Transfer-Encoding nor content-length header found";
        }
        http:Response res = new;
        res.setJsonPayload({"Outbound request content":value});
        _ = conn -> respond(res);
    }
}

Set response according to the request headers.

$ ballerina run http-disable-chunking.bal
ballerina: initiating service(s) in 'http-disable-chunking.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9092
ballerina: started HTTP/WS server connector 0.0.0.0:9090
$ curl http://localhost:9092/chunkingSample/
{"Outbound request content":"Lenght-18"}
To enable chunking, try changing the chunking option of the clientEndpoint to http:Chunking.ALWAYS.
{"Outbound request content":"chunked"}