ballerinaballerina-http

Ballerina - can we check response status code for a URL like www.google.com?


I am new to Ballerina, and i want to know if I can find the response status of any url. This is basically to check if the system is up or down.


Solution

  • Here is a slightly modified version of the Ballerina http client sample to demonstrate how to obtain the response status code.

    import ballerina/http;
    import ballerina/io;
    
    public function main() {
        http:Client clientEP = new ("http://www.mocky.io");
        var resp = clientEP->get("/v2/5ae082123200006b00510c3d/");
        if (resp is http:Response) {
            var payload = resp.getTextPayload();
            io:println(resp.statusCode); // print http status code
            if (payload is string) {
                io:println(payload);
            } else {
                io:println(payload.detail());
            }
        } else {
            io:println(resp.detail());
        }
    }
    

    This sample was taken from here.