the request sent by the client was syntactically incorrect when sending post requests
Asked Answered
A

3

5

The method in myController looks like this

@RequestMapping(value="/{processId}/dependents", method=RequestMethod.POST,consumes="application/json")
@ResponseBody
public Dependents postdependent(@ModelAttribute ProcessContext process,@RequestBody Dependent dependent) {
    return process.getDependents().addDependent(dependent);
}

My gets and delete work perfectly. But whenever I do a post I am getting the request sent by the client was syntactically incorrect. JSON for post request:

"{
   'dependentId' : '1003',
   'firstName'   : 'Vishu',
   'lastName'    : 'poodari',
   'birthDate'   : '1970/04/15'
}"

Please I tried all combinations by using single quotes, doubles quotes everything.

I am using rest-shell for doing the operations.

Please find my Dependent Class

public class Dependent {
    private String dependentId;
    private String firstName;
    private String lastName;
    private String birthDate;
    @JsonCreator
    public Dependent(@JsonProperty("dependentId") String dependentId, @JsonProperty("firstName") String firstName, @JsonProperty("lastName")String lastName,
            @JsonProperty("birthDate") String birthDate) {
        this.dependentId = dependentId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
    }
    public String getDependentId() {
        return dependentId;
    }
    public void setDependentId(String dependentId) {
        this.dependentId = dependentId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
}
Adala answered 26/2, 2014 at 18:28 Comment(4)
Show us your Dependent class. Also, turn your log level to debug and check the logs.Dispirited
Dependent class has been added.Adala
Are you sending a query string so that Spring can generate the @ModelAttribute? Where do you expect it to be generated from?Dispirited
I am sending a POST request in JSON formatAdala
V
12

syntactically incorrect means problem with the json,please replace the single quote with double.

{"dependentId" : "1003",
   "firstName"   : "Vishu",
   "lastName"    : "poodari",
   "birthDate"   : "1970/04/15"
}

also check the json keys should match with your Dependent class attribute names, and the data should be convertible by the parser.

Vitavitaceous answered 28/2, 2014 at 7:44 Comment(3)
Thanks for the response. I corrected my json and when i changed the log level to debug, it showed i am missing empty constructor. Now it works, Thanks all.Adala
"i changed the log level to debug, it showed i am missing empty constructor" Can you please tell me how that logs were added to the application.Periodicity
This error is occurring because default constructor missing in class add default constructor and everything will be working public Dependent() { }Micelle
M
5

Error *The request sent by the client was syntactically incorrect"** in most of the case means that jackson is not able to desalinize(convert json string to object) because default constructor is missing.

In your case there is missing default constructor, you have parameterized constructor which override default and jackson is not able create object

public Dependent(@JsonProperty("dependentId") String dependentId, @JsonProperty("firstName") String firstName, @JsonProperty("lastName")String lastName,
        @JsonProperty("birthDate") String birthDate) {      this.dependentId = dependentId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDate = birthDate;
}

Add also default constructor into your class and everything will be working

public Dependent() {
}
Micelle answered 8/10, 2016 at 21:1 Comment(0)
C
0

When using curl (on dos) i had the same problem. I needed to use all double quotes and therefore mask the ones within the body part: C:>curl -H "Content-Type: application/json" -X POST -d "{\"id\":1,\"firstName\":\"Hans\",\"lastName\":\"Muster\"}" http://localhost:8081/persons

Coexecutor answered 28/5, 2015 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.