Swan Lake Beta3 is here.
import ballerina/http;

// The HTTP client's chunking behavior can be configured as [CHUNKING_AUTO](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_AUTO),
// [CHUNKING_ALWAYS](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_ALWAYS),
// or [CHUNKING_NEVER](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_NEVER).
// In this example, it is set to `CHUNKING_NEVER`, which means that chunking never happens irrespective of the request size. 
// When chunking is set to `CHUNKING_AUTO`, chunking is done based on the request.
// [http1Settings](https://docs.central.ballerina.io/ballerina/http/latest/records/ClientHttp1Settings) annotation
// provides the chunking-related configurations.
final http:Client clientEndpoint = check new ("http://localhost:9090",
                        {http1Settings: {chunking: http:CHUNKING_NEVER}});

service / on new http:Listener(9092) {
    resource function get chunkingSample() returns json|error {
        //Invoke endpoint along with a JSON payload.
        json clientResponse =
            check clientEndpoint->post("/echo", {"name": "Ballerina"});
        return clientResponse;
    }
}

// A sample backend, which responds according to the chunking behavior.
service / on new http:Listener(9090) {
    resource function post echo(@http:Header{name:"Content-length"} string cLen)
             returns json {
        //Set the response with the content length.
        string value = "Length-" + cLen;
        return {"Outbound request content": value};
    }
}

Chunking

This sample demonstrates how to configure the chunking behavior of an http:Client. By default, the HTTP client sends messages with the content-length header. If the message size is larger than the buffer size (8K), messages are chunked. Chunking can be disabled using the client options.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
final http:Client clientEndpoint = check new ("http://localhost:9090",
                        {http1Settings: {chunking: http:CHUNKING_NEVER}});

The HTTP client’s chunking behavior can be configured as CHUNKING_AUTO, CHUNKING_ALWAYS, or CHUNKING_NEVER. In this example, it is set to CHUNKING_NEVER, which means that chunking never happens irrespective of the request size. When chunking is set to CHUNKING_AUTO, chunking is done based on the request. http1Settings annotation provides the chunking-related configurations.

service / on new http:Listener(9092) {
    resource function get chunkingSample() returns json|error {
        json clientResponse =
            check clientEndpoint->post("/echo", {"name": "Ballerina"});
        return clientResponse;
    }
}

Invoke endpoint along with a JSON payload.

service / on new http:Listener(9090) {
    resource function post echo(@http:Header{name:"Content-length"} string cLen)
             returns json {

A sample backend, which responds according to the chunking behavior.

        string value = "Length-" + cLen;
        return {"Outbound request content": value};
    }
}

Set the response with the content length.

bal run http_disable_chunking.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:9092
To use the client, execute the cURL command below. 
curl http://localhost:9092/chunkingSample
{"Outbound request content":"Length-20"}
import ballerina/http;

// The HTTP client's chunking behavior can be configured as [CHUNKING_AUTO](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_AUTO),
// [CHUNKING_ALWAYS](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_ALWAYS),
// or [CHUNKING_NEVER](https://docs.central.ballerina.io/ballerina/http/latest/constants#CHUNKING_NEVER).
// In this example, it is set to `CHUNKING_NEVER`, which means that chunking never happens irrespective of the request size. 
// When chunking is set to `CHUNKING_AUTO`, chunking is done based on the request.
// [http1Settings](https://docs.central.ballerina.io/ballerina/http/latest/records/ClientHttp1Settings) annotation
// provides the chunking-related configurations.
final http:Client clientEndpoint = check new ("http://localhost:9090",
                        {http1Settings: {chunking: http:CHUNKING_NEVER}});

service / on new http:Listener(9092) {
    resource function get chunkingSample() returns json|error {
        //Invoke endpoint along with a JSON payload.
        json clientResponse =
            check clientEndpoint->post("/echo", {"name": "Ballerina"});
        return clientResponse;
    }
}

// A sample backend, which responds according to the chunking behavior.
service / on new http:Listener(9090) {
    resource function post echo(@http:Header{name:"Content-length"} string cLen)
             returns json {
        //Set the response with the content length.
        string value = "Length-" + cLen;
        return {"Outbound request content": value};
    }
}

Chunking

This sample demonstrates how to configure the chunking behavior of an http:Client. By default, the HTTP client sends messages with the content-length header. If the message size is larger than the buffer size (8K), messages are chunked. Chunking can be disabled using the client options.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
final http:Client clientEndpoint = check new ("http://localhost:9090",
                        {http1Settings: {chunking: http:CHUNKING_NEVER}});

The HTTP client’s chunking behavior can be configured as CHUNKING_AUTO, CHUNKING_ALWAYS, or CHUNKING_NEVER. In this example, it is set to CHUNKING_NEVER, which means that chunking never happens irrespective of the request size. When chunking is set to CHUNKING_AUTO, chunking is done based on the request. http1Settings annotation provides the chunking-related configurations.

service / on new http:Listener(9092) {
    resource function get chunkingSample() returns json|error {
        json clientResponse =
            check clientEndpoint->post("/echo", {"name": "Ballerina"});
        return clientResponse;
    }
}

Invoke endpoint along with a JSON payload.

service / on new http:Listener(9090) {
    resource function post echo(@http:Header{name:"Content-length"} string cLen)
             returns json {

A sample backend, which responds according to the chunking behavior.

        string value = "Length-" + cLen;
        return {"Outbound request content": value};
    }
}

Set the response with the content length.

bal run http_disable_chunking.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:9092
To use the client, execute the cURL command below. 
curl http://localhost:9092/chunkingSample
{"Outbound request content":"Length-20"}
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand