import ballerina/http;

// Creates an HTTP client to interact with a remote endpoint.
// [followRedirects](https://docs.central.ballerina.io/ballerina/http/latest/records/FollowRedirects) record provides configurations associated with HTTP redirects.
http:Client clientEndpoint = check new ("http://localhost:9092", {
        followRedirects: {enabled: true, maxCount: 5}
    }
);

service / on new http:Listener(9090) {

    resource function get hello() returns string|error {
        // Sends a `GET` request to the specified endpoint and Retrieved the text payload from the response.
        string returnResult = check clientEndpoint->get("/redirect1");
        return "Response received : " + returnResult;
    }
}

service / on new http:Listener(9092) {

    resource function get redirect1(http:Caller caller) returns error? {
        http:Response res = new;
        // Sends a redirect response with a location.
        check caller->redirect(res,
            http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}

service /redirect2 on new http:Listener(9093) {

    resource function get .() returns string {
        // Sends a response to the caller.
        return "Hello World!";
    }
}

Redirects

This example demonstrates an HTTP redirect. To follow redirects when calling an external HTTP server using the Ballerina HTTP client connector, set followRedirect to true.

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

import ballerina/http;
http:Client clientEndpoint = check new ("http://localhost:9092", {
        followRedirects: {enabled: true, maxCount: 5}
    }
);

Creates an HTTP client to interact with a remote endpoint. followRedirects record provides configurations associated with HTTP redirects.

service / on new http:Listener(9090) {
    resource function get hello() returns string|error {
        string returnResult = check clientEndpoint->get("/redirect1");
        return "Response received : " + returnResult;
    }
}

Sends a GET request to the specified endpoint and Retrieved the text payload from the response.

service / on new http:Listener(9092) {
    resource function get redirect1(http:Caller caller) returns error? {
        http:Response res = new;
        check caller->redirect(res,
            http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}

Sends a redirect response with a location.

service /redirect2 on new http:Listener(9093) {
    resource function get .() returns string {
        return "Hello World!";
    }
}

Sends a response to the caller.

bal run http_redirects.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:9092
[ballerina/http] started HTTP/WS listener 0.0.0.0:9093
# Invoke the service using the "cURL" command below.
curl -v http://localhost:9090/hello/
*   Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /hello/ HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-type: text/plain
< content-length: 32
< server: ballerina
< date: Wed, 2 Jun 2021 11:03:08 +0530
< 
* Connection #0 to host localhost left intact
Response received : Hello World!