Reputation: 645
I'm following the guide at http://spring.io/guides/gs/accessing-data-rest/ , which is like the hello world for this... GET requests works perfectly, but when trying the POST they suggest (I'm using Rest Client Chrome extension), with json
{ "firstName" : "Frodo", "lastName" : "Baggins" }
I get the following error:
{
cause: {
cause: null
message: "Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN at [Source: org.apache.catalina.connector.CoyoteInputStream@29508db1; line: 1, column: 11]"
}
message: "Could not read JSON: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN at [Source: org.apache.catalina.connector.CoyoteInputStream@29508db1; line: 1, column: 11]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN at [Source: org.apache.catalina.connector.CoyoteInputStream@29508db1; line: 1, column: 11]"
}
How can I fix this? Why is it happening?
Upvotes: 1
Views: 2701
Reputation: 880
I've had some issues whit POST method and Spring, when Cross-Site Request Forgery (csrf) protection is enabled (in spring security - <csrf/>
). The issue (that I had) was that the error doesn't seem to indicate this was the problem but adding the following to my forms did the trick:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
Upvotes: 0
Reputation: 1
JSON.stringify() the content or just put single quotes, that fixes the problem.
$.ajax({
url : 'http://localhost:8080/people',
type: 'POST',
dataType: 'json',
data: '{"firstName":"xx","lastName":"xx"}',
contentType: 'application/json'
});
Upvotes: 0
Reputation: 780
Try to verify if you had Content-Type:application/json when you send the request
Upvotes: 1