import ballerina/http;
import ballerina/io;
import ballerina/mime;
// Creates an endpoint for the [client](https://docs.central.ballerina.io/ballerina/http/latest/clients/Client).
http:Client clientEndpoint = check new ("http://localhost:9090");
service /'stream on new http:Listener(9090) {
resource function get fileupload() returns http:Response|error? {
http:Request request = new;
//[Sets the file](https://docs.central.ballerina.io/ballerina/http/latest/classes/Request#setFileAsPayload) as the request payload.
request.setFileAsPayload("./files/BallerinaLang.pdf",
contentType = mime:APPLICATION_PDF);
//Sends the request to the client with the file content.
http:Response clientResponse =
check clientEndpoint->post("/stream/receiver", request);
// forward received response to caller
return clientResponse;
}
resource function post receiver(http:Caller caller,
http:Request request) returns error? {
//[Retrieve the byte stream](https://docs.central.ballerina.io/ballerina/http/latest/classes/Request#getByteStream).
stream<byte[], io:Error?> streamer = check request.getByteStream();
//Writes the incoming stream to a file using `io:fileWriteBlocksFromStream` API by providing the file location to which the content should be written to.
check io:fileWriteBlocksFromStream(
"./files/ReceivedFile.pdf", streamer);
check streamer.close();
check caller->respond("File Received!");
}
}
StreamingBallerina supports HTTP input and output streaming capability based on the Ballerina |
import ballerina/http;
import ballerina/io;
import ballerina/mime;
http:Client clientEndpoint = check new ("http://localhost:9090");
Creates an endpoint for the client.
service /'stream on new http:Listener(9090) {
resource function get fileupload() returns http:Response|error? {
http:Request request = new;
request.setFileAsPayload("./files/BallerinaLang.pdf",
contentType = mime:APPLICATION_PDF);
Sets the file as the request payload.
http:Response clientResponse =
check clientEndpoint->post("/stream/receiver", request);
Sends the request to the client with the file content.
return clientResponse;
}
forward received response to caller
resource function post receiver(http:Caller caller,
http:Request request) returns error? {
stream<byte[], io:Error?> streamer = check request.getByteStream();
check io:fileWriteBlocksFromStream(
"./files/ReceivedFile.pdf", streamer);
check streamer.close();
check caller->respond("File Received!");
}
}
Writes the incoming stream to a file using io:fileWriteBlocksFromStream
API by providing the file location to which the content should be written to.
# In the directory, which contains the `.bal` file, create a directory named `files`,
# and add a PDF file named `BallerinaLang.pdf` in it.
bal run http_streaming.bal
#Send a request to the streaming service.
curl -X GET http://localhost:9090/stream/fileupload
File Received!