import ballerina/http;
import ballerina/mime;
import ballerina/file;endpoint http:Listener multipartEP {
port:9092
};@http:ServiceConfig {basePath:"/multiparts"}
service<http:Service> test bind multipartEP {
@http:ResourceConfig {
methods:["GET"],
path:"/encode_out_response"
}
multipartSender (endpoint conn, http:Request request) {
mime:Entity parentPart = new;
mime:MediaType mixedContentType = mime:getMediaType(mime:MULTIPART_MIXED);
parentPart.contentType = mixedContentType;
mime:Entity childPart1 = new;
mime:MediaType contentTypeOfJsonPart = mime:getMediaType(mime:APPLICATION_JSON);
childPart1.contentType = contentTypeOfJsonPart;
childPart1.setJson({"name":"wso2"});
mime:Entity childPart2 = new;
mime:MediaType contentTypeOfFilePart = mime:getMediaType(mime:TEXT_XML);
childPart2.contentType = contentTypeOfFilePart;
file:Path fileHandler = new("./files/test.xml");
childPart2.setFileAsEntityBody(fileHandler);
mime:Entity[] childParts = [childPart1, childPart2];
parentPart.setBodyParts(childParts);
mime:Entity[] immediatePartsToResponse = [parentPart];
http:Response outResponse = new;
outResponse.setMultiparts(immediatePartsToResponse, mime:MULTIPART_FORM_DATA); _ = conn -> respond(outResponse);
}
}
Outbound Response with MultipartsBallerina supports encoding multipart content along with its nested parts to be sent via a Response. |
|
import ballerina/http;
import ballerina/mime;
import ballerina/file;
|
|
endpoint http:Listener multipartEP {
port:9092
};
|
|
@http:ServiceConfig {basePath:"/multiparts"}
service<http:Service> test bind multipartEP {
@http:ResourceConfig {
methods:["GET"],
path:"/encode_out_response"
}
multipartSender (endpoint conn, http:Request request) {
|
|
mime:Entity parentPart = new;
mime:MediaType mixedContentType = mime:getMediaType(mime:MULTIPART_MIXED);
parentPart.contentType = mixedContentType;
|
Create an enclosing entity to hold child parts. |
mime:Entity childPart1 = new;
mime:MediaType contentTypeOfJsonPart = mime:getMediaType(mime:APPLICATION_JSON);
childPart1.contentType = contentTypeOfJsonPart;
childPart1.setJson({"name":"wso2"});
|
Create a child part with json content. |
mime:Entity childPart2 = new;
mime:MediaType contentTypeOfFilePart = mime:getMediaType(mime:TEXT_XML);
childPart2.contentType = contentTypeOfFilePart;
|
Create another child part with a file. |
file:Path fileHandler = new("./files/test.xml");
childPart2.setFileAsEntityBody(fileHandler);
|
This file path is relative to where the ballerina is running. If your file is located outside, please give the absolute file path instead. |
mime:Entity[] childParts = [childPart1, childPart2];
|
Create an array to hold child parts. |
parentPart.setBodyParts(childParts);
|
Set the child parts to the parent part. |
mime:Entity[] immediatePartsToResponse = [parentPart];
http:Response outResponse = new;
outResponse.setMultiparts(immediatePartsToResponse, mime:MULTIPART_FORM_DATA);
|
Create an array to hold the parent part and set it to response. |
_ = conn -> respond(outResponse);
}
}
|
|
$ ballerina run outbound-response-with-multiparts.bal
|
|
$ curl -X GET http://localhost:9092/multiparts/encode_out_response
|
|