1

I have the following JSON , which I have validated using JsonLint :

{
        "names": [
            {
                "id":"123",
                "name":"stubName",
                "type":"stubType",
            }
        ]
    }

I would like to add it as a message body in my CURL command (using Cygwin) in order to test a REST handler that I wrote.

I am currently using this command, which I created from reading this answer:

 curl -H "Content-Type: application/json" -X PUT -d '{
        "names": [
            {
                "id":"123",
                "name":"StubName",
                "type":"StubType",
            }
        ]
    }' localhost:9980/Id/123/version/123/addPerson

My handler is as follows:

            @PUT
            @Consumes("application/json")
            @Path("/Id/{Id}/version/{version}/addPerson")
            public Response addPerson(@PathParam("Id") String Id,
                                                    @PathParam("version") String version, 
                                                     @Context List<Name> names) {

            LOGGER.info("NAMES PASSED : {}", names.toString());

    }

When I try to hit my method using CURL I get an HTTP 400 method.

Edit: when I try to do names.toString() I get a null pointer, meaning that the names list must be null.

Community
  • 1
  • 1
java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

1
  1. You should not use @Context The entity body (parameter) should be annotation-less.

     public Response addPerson(@PathParam("Id") String Id,
                               @PathParam("version") String version, 
                               List<Name> names) {
    
  2. With a List<Name> you should be sending a JSON array. Get rid of the {"names":, and just send the []

    curl -H "Content-Type: application/json" -X PUT -d '[
        {
            "id":"123",
            "name":"StubName",
            "type":"StubType",
        }
    ]' localhost:9980/Id/123/version/123/addPerson
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720