import ballerina/http;

service on new http:Listener(9092) {
    // The `consumes` and `produces` annotations of the [resource configuration](https://docs.central.ballerina.io/ballerina/http/latest/records/HttpResourceConfig)
    // contain MIME types as an array of strings. The resource can only consume/accept `text/json` and
    // `application/json` media types. Therefore, the `Content-Type` header
    // of the request must be in one of these two types. The resource can produce
    // `application/xml` payloads. Therefore, you need to set the `Accept` header accordingly.
    @http:ResourceConfig {
        consumes: ["text/json", "application/json"],
        produces: ["application/xml"]
    }
    resource function post infoService(@http:Payload json msg)
            returns xml|http:InternalServerError {
        json|error nameString = msg.name;
        if (nameString is json) {
            xml name = xml `<name>${<string>nameString}</name>`;
            return name;
        }
        return { body: "Invalid json: `name` not present"};
    }
}

Restrict By Media Type

You can configure resources of HTTP services to restrict the types of media they consume and produce. This is done through the consumes and produces annotation attributes of the ResourceConfig annotation, which is used with resources.

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

import ballerina/http;
service on new http:Listener(9092) {
    @http:ResourceConfig {
        consumes: ["text/json", "application/json"],
        produces: ["application/xml"]
    }
    resource function post infoService(@http:Payload json msg)
            returns xml|http:InternalServerError {
        json|error nameString = msg.name;
        if (nameString is json) {
            xml name = xml `<name>${<string>nameString}</name>`;
            return name;
        }
        return { body: "Invalid json: `name` not present"};
    }
}

The consumes and produces annotations of the resource configuration contain MIME types as an array of strings. The resource can only consume/accept text/json and application/json media types. Therefore, the Content-Type header of the request must be in one of these two types. The resource can produce application/xml payloads. Therefore, you need to set the Accept header accordingly.

bal run restrict_by_media_type.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9092
# To invoke the service, execute the following cURL request.
curl -v http://localhost:9092/infoService -H "Accept:application/xml" -H "Content-Type:application/json" -d '{"name":"Ballerina"}'
> POST /infoService HTTP/1.1
> Host: localhost:9092
> User-Agent: curl/7.64.1
> Accept:application/xml
> Content-Type:application/json
> Content-Length: 20
>
* upload completely sent off: 20 out of 20 bytes
< HTTP/1.1 200 OK
< content-type: application/xml
< content-length: 22
< server: ballerina
< date: Wed, 23 Sep 2020 10:25:55 +0530
<
* Connection #0 to host localhost left intact
<name>Ballerina</name>* Closing connection 0
# To invoke the service using unsupported media type, execute the following cURL request. The content type of the
# request is not listed under the `consumes` resource configuration.
curl -v http://localhost:9092/infoService -H "Accept:application/xml" -H "Content-Type:text/plain" -d "Hello ballerina"
> POST /infoService HTTP/1.1
> Host: localhost:9092
> User-Agent: curl/7.64.1
> Accept:application/xml
> Content-Type:text/plain
> Content-Length: 15
>
* upload completely sent off: 15 out of 15 bytes
< HTTP/1.1 415 Unsupported Media Type
< content-type: text/plain
< content-length: 0
< server: ballerina
< date: Wed, 23 Sep 2020 10:26:50 +0530
<
* Connection #0 to host localhost left intact
* Closing connection 0
# To invoke the service with a media type that is not acceptable, execute the following cURL request. The media type mentioned
# in the Accept header is not listed under the `produces` resource configuration.
curl -v http://localhost:9092/infoService -H "Accept:text/html" -H "Content-Type:application/json" -d '{"name":"Ballerina"}'
> POST /infoService HTTP/1.1
> Host: localhost:9092
> User-Agent: curl/7.64.1
> Accept:text/html
> Content-Type:application/json
> Content-Length: 20
>
* upload completely sent off: 20 out of 20 bytes
< HTTP/1.1 406 Not Acceptable
< content-type: text/plain
< content-length: 0
< server: ballerina
< date: Wed, 23 Sep 2020 10:27:28 +0530
<
* Connection #0 to host localhost left intact
* Closing connection 0