import ballerina/io;
import ballerina/http;
endpoint http:Listener helloWorldEP {
    port:9090
};@http:ServiceConfig {
          basePath: "/hello"
}
service<http:Service> helloWorld bind helloWorldEP{    @http:ResourceConfig {
        path:"/"
    }
    hello (endpoint outboundEP, http:Request request) {
        if (request.expects100Continue()) {
            _ = outboundEP -> continue();
        }
        http:Response res = new;
        match request.getStringPayload() {
            string payload => {
                io:println(payload);
                res.statusCode =200;
                res.setStringPayload("Hello World!\n");
                _ = outboundEP -> respond(res);
            }
            error payloadError =>  {
                res.statusCode = 500;
                res.setStringPayload(payloadError.message);
                _ = outboundEP -> respond(res);
            }
        }
    }
}

HTTP 100 Continue

Convenience functions are provided in the HTTP library for ease of use when handling 100-continue scenarios.

import ballerina/io;
import ballerina/http;
endpoint http:Listener helloWorldEP {
    port:9090
};

Attributes associated with the service endpoint is defined here.

@http:ServiceConfig {
          basePath: "/hello"
}
service<http:Service> helloWorld bind helloWorldEP{
    @http:ResourceConfig {
        path:"/"
    }
    hello (endpoint outboundEP, http:Request request) {
        if (request.expects100Continue()) {

Check if the client expects a 100-continue response.

            _ = outboundEP -> continue();
        }

Send a 100-continue response to the client.

        http:Response res = new;
        match request.getStringPayload() {
            string payload => {
                io:println(payload);
                res.statusCode =200;
                res.setStringPayload("Hello World!\n");
                _ = outboundEP -> respond(res);
            }
            error payloadError =>  {
                res.statusCode = 500;
                res.setStringPayload(payloadError.message);
                _ = outboundEP -> respond(res);
            }
        }
    }
}

The client will start sending the payload once it receives the 100-continue response. Get this payload sent by the client.

$ ballerina run http-expect-header.bal
ballerina: initiating service(s) in 'http-expect-header.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
TEST 100 CONTINUE
$ curl -v -d "TEST 100 CONTINUE" -H'Expect:100-continue' http://localhost:9090/hello
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9090 (#0)
> POST /hello HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.47.0
> Accept: */*
> Expect:100-continue
> Content-Length: 17
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 13
<
Hello World!
* Connection #0 to host localhost left intact