import ballerina/http;
import ballerina/mime;
import ballerina/io;
endpoint http:Listener multipartEP {
    port:9090
};
@http:ServiceConfig {basePath:"/multiparts"}
service<http:Service> test bind multipartEP {
    @http:ResourceConfig {
        methods:["POST"],
        path:"/decode_in_request"
    }
    receiveMultiparts (endpoint conn, http:Request request) {
        http:Response response = new;
        match request.getMultiparts() {
            mime:EntityError err => {
                io:println(err);
                response.setStringPayload("Error in decoding multiparts!");
                response.statusCode = 500;
            }
            mime:Entity[] bodyParts => {
                int i = 0;
                io:println("Body Parts Detected!");
                while (i < lengthof bodyParts) {
                    mime:Entity part = bodyParts[i];
                    handleContent(part);
                    i = i + 1;
                }
                response.setStringPayload("Multiparts Received!");
            }
        }
        _ = conn -> respond(response);
    }
}
function handleContent (mime:Entity bodyPart) {
    string contentType = bodyPart.contentType.toString();
    if (mime:APPLICATION_XML == contentType || mime:TEXT_XML == contentType) {
        var payload = bodyPart.getXml();
        match payload {
            mime:EntityError err => io:println("Error in getting xml payload");
            xml xmlContent => io:println(xmlContent);
        }
    } else if (mime:APPLICATION_JSON == contentType) {
        var payload = bodyPart.getJson();
        match payload {
            mime:EntityError err => io:println("Error in getting json payload");
            json jsonContent => io:println(jsonContent);
        }
    } else if (mime:TEXT_PLAIN == contentType) {
        var payload = bodyPart.getText();
        match payload {
            mime:EntityError err => io:println("Error in getting string payload");
            string textContent => io:println(textContent);
        }
    }
}

Inbound Request with Multiparts

Multipart request handling is supported by the Ballerina server connector. When you request multiparts from the HTTP inbound request, you get an array of body parts (an array of entities). You can loop through this array and handle the received body parts according to your requirement.

import ballerina/http;
import ballerina/mime;
import ballerina/io;
endpoint http:Listener multipartEP {
    port:9090
};

Creating a listener for the service.

@http:ServiceConfig {basePath:"/multiparts"}
service<http:Service> test bind multipartEP {
    @http:ResourceConfig {
        methods:["POST"],
        path:"/decode_in_request"
    }

Binding the listener to the service.

    receiveMultiparts (endpoint conn, http:Request request) {
        http:Response response = new;

This resource accepts multipart requests.

        match request.getMultiparts() {

Extract the bodyparts from the request.

            mime:EntityError err => {
                io:println(err);
                response.setStringPayload("Error in decoding multiparts!");
                response.statusCode = 500;
            }

Setting the error response in case of an error

            mime:Entity[] bodyParts => {
                int i = 0;
                io:println("Body Parts Detected!");
                while (i < lengthof bodyParts) {
                    mime:Entity part = bodyParts[i];
                    handleContent(part);
                    i = i + 1;
                }
                response.setStringPayload("Multiparts Received!");
            }
        }
        _ = conn -> respond(response);
    }
}

Iterate through the body parts.

function handleContent (mime:Entity bodyPart) {
    string contentType = bodyPart.contentType.toString();
    if (mime:APPLICATION_XML == contentType || mime:TEXT_XML == contentType) {

The content logic that handles the body parts vary based on your requirement.

        var payload = bodyPart.getXml();
        match payload {
            mime:EntityError err => io:println("Error in getting xml payload");
            xml xmlContent => io:println(xmlContent);
        }
    } else if (mime:APPLICATION_JSON == contentType) {

Extract the xml data from the body part and print it.

        var payload = bodyPart.getJson();
        match payload {
            mime:EntityError err => io:println("Error in getting json payload");
            json jsonContent => io:println(jsonContent);
        }
    } else if (mime:TEXT_PLAIN == contentType) {

Extract the json data from the body part and print it.

        var payload = bodyPart.getText();
        match payload {
            mime:EntityError err => io:println("Error in getting string payload");
            string textContent => io:println(textContent);
        }
    }
}

Extract the text data from the body part and print it.

$ ballerina run inbound-request-with-multiparts.bal
$ curl -v -F key1=value1 -F upload=/home/User/Downloads/samplefile.txt http://localhost:9090/multiparts/receivableParts