1

I'm new to Ballerina and am encountering an error. I have made a service and am using Postman and the terminal using the curl command, but I keep getting this error:

curl: (7) Failed to connect to localhost port 8080 after 0 ms: Connection refused

Here is my code. It is correct if I'm not mistaken

import ballerina/http;
import ballerina/io;


type User record {
    readonly string id;
    string name;
    int age;
};

table<User> key(id) userTable = table[];

service /on new http:Listener(8080) {
    resource function post createUser(User user) returns string {
        userTable.add(user);    
        //if you want to use io  
        io:println("user saved sucesfully: " + user.toString());
        return "user saved successfully";
    }

    resource function get getAllUsers()returns User[] {
        return  userTable.toArray();
    }
}

these are the curl commands I've used

curl -X POST -H "Content-Type: application/json" -d '{"id": "1", "name": "John", "age": 30}' http://localhost:8080/createUser
curl http://localhost:8080/getAllUsers
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • I just tried your code, and the curl commands with Ballerina Swan Lake Update 7.2 on Mac work as expected. What is your environment (OS and Ballerina version)? Also, can you share the CLI output as well? – Sameera Jayasoma Aug 30 '23 at 23:22

0 Answers0