import ballerina/http;
import ballerina/io;
http:Client clientEndpoint = new("http://postman-echo.com");public function main() {
    io:println("GET request:");
    var response = clientEndpoint->get("/get?test=123");
    handleResponse(response);    io:println("\nPOST request:");
    response = clientEndpoint->post("/post", "POST: Hello World");
    handleResponse(response);    io:println("\nUse custom HTTP verbs:");
    response = clientEndpoint->execute("COPY", "/get", "CUSTOM: Hello World");
    http:Request req = new;
    req.addHeader("Sample-Name", "http-client-connector");
    response = clientEndpoint->get("/get", req);
    if (response is http:Response) {
        string contentType = response.getHeader("Content-Type");
        io:println("Content-Type: " + contentType);        int statusCode = response.statusCode;
        io:println("Status code: " + statusCode.toString());    } else {
        io:println("Error when calling the backend: " , response.reason());
    }
}
function handleResponse(http:Response|error response) {
    if (response is http:Response) {
        var msg = response.getJsonPayload();
        if (msg is json) {
            io:println(msg.toJsonString());
        } else {
            io:println("Invalid payload received:" , msg.reason());
        }
    } else {
        io:println("Error when calling the backend: ", response.reason());
    }
}

Client Endpoint

The HTTP Client Connector can be used to connect to and interact with an HTTP server.

import ballerina/http;
import ballerina/io;
http:Client clientEndpoint = new("http://postman-echo.com");

Creates a new client with the backend URL.

public function main() {
    io:println("GET request:");
    var response = clientEndpoint->get("/get?test=123");

Sends a GET request to the specified endpoint.

    handleResponse(response);

Handles the response.

    io:println("\nPOST request:");
    response = clientEndpoint->post("/post", "POST: Hello World");

Sends a POST request to the specified endpoint.

    handleResponse(response);

Handles the response.

    io:println("\nUse custom HTTP verbs:");
    response = clientEndpoint->execute("COPY", "/get", "CUSTOM: Hello World");

Uses the execute() remote function for custom HTTP verbs.

    http:Request req = new;
    req.addHeader("Sample-Name", "http-client-connector");

Initializes a request.

    response = clientEndpoint->get("/get", req);
    if (response is http:Response) {
        string contentType = response.getHeader("Content-Type");
        io:println("Content-Type: " + contentType);

The get(), head(), and options() can have the optional message parameter, which will be a request or a payload.

        int statusCode = response.statusCode;
        io:println("Status code: " + statusCode.toString());
    } else {
        io:println("Error when calling the backend: " , response.reason());
    }
}
function handleResponse(http:Response|error response) {
    if (response is http:Response) {
        var msg = response.getJsonPayload();
        if (msg is json) {

The below function handles the response received from the remote HTTP endpoint.

            io:println(msg.toJsonString());
        } else {
            io:println("Invalid payload received:" , msg.reason());
        }
    } else {
        io:println("Error when calling the backend: ", response.reason());
    }
}

Prints the received json response.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run http_client_endpoint.bal
GET request:
{"args":{"test":"123"}, "headers":{"x-forwarded-proto":"https", "host":"postman-echo.com", "user-agent":"ballerina/1.0.0-beta", "x-forwarded-port":"80"}, "url":"https://postman-echo.com/get?test=123"}
POST request:
{"args":{}, "data":"POST: Hello World", "files":{}, "form":{}, "headers":{"x-forwarded-proto":"https", "host":"postman-echo.com", "content-length":"17", "content-type":"text/plain", "user-agent":"ballerina/1.0.0-beta", "x-forwarded-port":"80"}, "json":null, "url":"https://postman-echo.com/post"}
Use custom HTTP verbs:
Content-Type: application/json; charset=utf-8
Status code: 200