import ballerina/http;endpoint http:Listener passthroughEP {
port:9090
};endpoint http:SimpleClientEndpoint clientEP {
url:"http://localhost:9092/hello"
};service<http:Service> passthrough bind passthroughEP {
@http:ResourceConfig {
path:"/"
}
passthrough (endpoint outboundEP, http:Request req) {
var clientResponse = clientEP -> forward("/", req);
match clientResponse {
http:Response res => {
_ = outboundEP -> respond(res);
}
http:HttpConnectorError err => {
http:Response res = new;
res.statusCode = 500;
res.setStringPayload(err.message);
_ = outboundEP -> respond(res);
}
}
}
}endpoint http:Listener helloEP {
port:9092
};
service<http:Service> hello bind helloEP {
@http:ResourceConfig {
methods:["POST", "PUT", "GET"],
path:"/"
}
helloResource (endpoint outboundEP, http:Request req) {
http:Response res = new;
res.setStringPayload("Hello World!");
_ = outboundEP -> respond(res);
}
}
PassthroughThe passthrough sample exhibits the process of HTTP client connector. Echo Service is used as a sample backend. |
|
import ballerina/http;
|
|
endpoint http:Listener passthroughEP {
port:9090
};
|
|
endpoint http:SimpleClientEndpoint clientEP {
url:"http://localhost:9092/hello"
};
|
|
service<http:Service> passthrough bind passthroughEP {
|
|
@http:ResourceConfig {
path:"/"
}
passthrough (endpoint outboundEP, http:Request req) {
|
The passthrough resource allows all HTTP methods since the resource configuration does not explicitly specify which HTTP methods are allowed. |
var clientResponse = clientEP -> forward("/", req);
|
Calling forward() on the backend client endpoint forwards the request the passthrough resource received to the backend. When forwarding, the request is made using the same HTTP method used to invoke the passthrough resource. The forward() function returns the response from the backend if there weren’t any errors. |
match clientResponse {
http:Response res => {
|
Since forward() can return an error as well, a matcher is required to handle the two cases. |
_ = outboundEP -> respond(res);
}
http:HttpConnectorError err => {
|
If the request was successful, an HTTP response will be returned. Here, the received response is forwarded to the client through the outbound endpoint. |
http:Response res = new;
res.statusCode = 500;
res.setStringPayload(err.message);
_ = outboundEP -> respond(res);
}
}
}
}
|
If there was an error, it is used to construct a 500 response and this is sent back to the client. |
endpoint http:Listener helloEP {
port:9092
};
|
|
service<http:Service> hello bind helloEP {
|
Sample hello world service. |
@http:ResourceConfig {
methods:["POST", "PUT", "GET"],
path:"/"
}
helloResource (endpoint outboundEP, http:Request req) {
http:Response res = new;
res.setStringPayload("Hello World!");
_ = outboundEP -> respond(res);
}
}
|
The helloResource only accepts requests made using the specified HTTP methods |
$ ballerina run passthrough.bal
ballerina: initiating service(s) in 'passthrough.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
|
|
$ curl -v http://localhost:9090/passthrough -X POST
Resource is invoked
$ curl -v http://localhost:9090/passthrough -X GET
Resource is invoked
$ curl -v http://localhost:9090/passthrough -X PUT
Resource is invoked
|
|