1

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

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57
  • 1
    I tried with Ballerina 1.2 and it is working for me. I get the output `aaa=amaval bbb=bbbval ccc=amaval ddd=bbb val` – Sameera Jayasoma Mar 26 '20 at 20:21
  • Works with Ballerina 1.2.0 when I added the missing service definition. The code you show us can't be used to reproduce the problem. – user272735 Mar 27 '20 at 06:52
  • @user272735 Do you mean that this was a known issue fixed with 1.2.0. (I use 1.1.3). It would be helpful if you can express the reason, for not being able to use my above code to reproduce the issue. And can you please let me know, what was the missing service definition? – Samitha Chathuranga Mar 28 '20 at 12:27
  • @SameeraJayasoma, Isn't this working in Ballerina 1.1.3 ? I am using 1.1.3 and doesn't work for me. (I updated the question with the version; sorry couldn't mention it earlier) – Samitha Chathuranga Mar 28 '20 at 12:30
  • BTW I tested again and found this working. The issue had been with the IntelliJ Idea debugger. I was running in debug mode and seeing that 'jsonarray' object was getting 'null' value, hence had assumed the code was not running correctly. I think there are still such these known issues in debugger. Thanks for the help. – Samitha Chathuranga Mar 28 '20 at 17:06

0 Answers0