import ballerina/http;

http:Client clientEP = check new ("http://postman-echo.com");

service / on new http:Listener(9090) {

    // The passthrough resource allows all HTTP methods as the accessor is `default`.
    resource function 'default passthrough(http:Request req)
            returns http:Response|error? {
        // When [forward()](https://docs.central.ballerina.io/ballerina/http/latest/clients/Client#forward) is called on the backend client endpoint, it forwards the request that the passthrough
        // resource received to the backend. When forwarding, the request is made using the same HTTP method that was
        // used to invoke the passthrough resource. The `forward()` function returns the response from the backend if
        // there are no errors.
        http:Response response = check clientEP->forward("/get", req);
        return response;
    }
}

Passthrough

The passthrough sample exhibits the process of an HTTP client connector. The ‘Echo Service’ is used as a sample backend.

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

import ballerina/http;
http:Client clientEP = check new ("http://postman-echo.com");
service / on new http:Listener(9090) {
    resource function 'default passthrough(http:Request req)
            returns http:Response|error? {

The passthrough resource allows all HTTP methods as the accessor is default.

        http:Response response = check clientEP->forward("/get", req);
        return response;
    }
}

When forward() is called on the backend client endpoint, it forwards the request that the passthrough resource received to the backend. When forwarding, the request is made using the same HTTP method that was used to invoke the passthrough resource. The forward() function returns the response from the backend if there are no errors.

bal run passthrough.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# To invoke the service, use the cURL commands below.
curl http://localhost:9090/passthrough
{"args":{}, "headers":{"x-forwarded-proto":"http", "x-forwarded-port":"80", "host":"postman-echo.com", "x-amzn-trace-id":"Root=1-60b7255d-23ce05a61ad55a0164ca19d3", "accept":"*/*", "user-agent":"ballerina"}, "url":"http://postman-echo.com/get"}