maryo
maryo

Reputation: 45

Grails - JSON param request not passing to controller

I would like to pass a JSON to my requete. But not possible to find the JSON.

@Secured(['ROLE_STUDENT'])
def validate() {
    println params
    println request.JSON
}

Result :

[controller:student, action:validate, id:2345444]
[:]

URL :

curl -i -H "Content-Type: application/json" -X POST -d '{"studentScores":[{"id":"2","score":"17"}],"comment":"good"}' 'http://localhost:8080/myapp/studient/2345444/validate' -H 'Cookie: JSESSIONID=3CCDC701553A2208428BB7135DDA5546'

I tried with GET and POST, and with à JQuery

$.ajax({
    url: urlSubmit,
    type: 'POST', 
    data: JSON.stringify(student),
    contentType:"application/json; charset=utf-8",
    dataType: "json"
});

Grails 2.4.4 Java 8 (compliance level java 7) Tomcat 8

Some plugins : spring-security-core:2.0-RC4 spring-security-facebook:0.16.2 spring-security-oauth2-provider:2.0-RC2

Need your help

Upvotes: 0

Views: 893

Answers (2)

Koloritnij
Koloritnij

Reputation: 1217

For first u have an error in your url in curl :)

http://localhost:8080/myapp/STUDIENT/2345444/validate

try this code

def json = JSON.stringify(params)

but I don't sure.

Upvotes: 0

Victor
Victor

Reputation: 5131

See this: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html (9.3 GET).

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI

Request-URI being the important part here. There is no concept of body data in a GET requests.

So, change to POST and your request should work.

Upvotes: 1

Related Questions