How to access and process json array sent to a service resource in Ballerina

193 Views Asked by At

I have a POST resource and I want to pass a JSON array as the request payload.

   @http:ResourceConfig {
    methods: ["POST"],
    path: "/news-articles/validatetest",
    cors: {
        allowOrigins: ["*"],
        allowHeaders: ["Authorization, Lang"]
    },
    produces: ["application/json"],
    consumes: ["application/json"]
    }
    resource function validateArticlesTest(http:Caller caller, http:Request req) {
        json[]|error jsonarray = <json[]>req.getJsonPayload();
        io:println(jsonarray);
    }

My request is as below.

curl -X POST http://localhost:9090/news-articles/validatetest -H "Content-Type: application/json" --data '[{"aaa":"amaval", "bbb":"bbbval"},{"ccc":"amaval", "ddd":"bbb  val"}]'

But 'jsonarray' gets always null when I run this and make the above curl request.

I guess I am not doing this correct. What is the correct approach to achieve this?

Edit: (Adding the version) Ballerina version: jBallerina 1.1.3

0

There are 0 best solutions below