Auro
Auro

Reputation: 1628

POST request to RESTful API always results in HTTP 400 Syntactically incorrect error message

I've implemented a RESTful API in my web server using Jersey to handle simple POST requests.

The method is given below: -

@Path("create")
public class Create {

.........

@POST
    @Path("/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public StatusResponse createPost(
            @PathParam("id") final String identifier,
                                                 final PostInfo postInfo) {

        // Does its work 

    }

And following is the PostInfo.java : -

    public class PostInfo {

        private String message;
        private String date;

        public PostInfo() {

            message = null;
            date = null;

        }

        /**
         * @return the message
         */
        public String getMessage() {
            return message;
        }

        /**
         * @return the date
         */
        public String getDate() {
            return date;
        }

        /**
         * @param message the message to set
         */
        public void setMessage(final String message) {
            this.message = message;
        }

        /**
         * @param date the date to set
         */
        public void setDate(final String date) {
            this.date = date;
        }

  }

Problem here is, every time I make a POST request with JSON elements, it results in

HTTP Status 400 - Bad Request The request sent by the client was syntactically incorrect.

I had a look at a few others posts having similar problem (this and this), but they were of no help.

I'm using Chrome's POSTMan to make the request at the following URL with the following body:

URL: wallpostapi.herokuapp.com/webapi/create/[email protected] Body :

{
    "message"   :   "WHADDUP DAWG???"
    "date"      :   "2016-09-09 11:00:20"
}

Upvotes: 0

Views: 623

Answers (2)

Kris
Kris

Reputation: 1734

The syntax of your JSON is not correct, a comma missing. Please try the following JSON.

{
    "message": "WHADDUP DAWG???",
    "date": "2016-09-09 11:00:20"
}

Further, the best practice is to use nouns but no verbs in the URI like,

http://wallpostapi.herokuapp.com/webapi/v1/info

The ID can be send in the header as an optional parameter or can be auto generated for create.

id   [email protected]

The name of your create method should be,

public StatusResponse createInfo(
            @HeaderParam("id") final String identifier,
            final PostInfo  postInfo {
        // Does its work 
} 

Upvotes: 1

Christian Alt-Wibbing
Christian Alt-Wibbing

Reputation: 357

BTW: the HTTP specifikation descripes a POST to create an subentry of an list. So the POST should go to the uri wallpostapi.herokuapp.com/webapi/create

if you know the id you should use the PUT Method of HTTP

Upvotes: 0

Related Questions