Sebastien
Sebastien

Reputation: 4295

Json parsing error when GETting grails controller action with empty body

When I send a GET request with an empty body to my grails 1.3.7 controller by using the .json file extension (eg http://localhost:8080/myapp/mycontroller/myaction.json) I get a request parsing exception and it seems that grails is trying to parse my empty body to JSON. If I send the same request to the same action but without the .json extension, I don't have any error.

How can I get rid of this error?

Upvotes: 2

Views: 1185

Answers (2)

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

My best stab at this is to have separate clauses in the URL mapping and making sure that for GETtish requests, parseRequest is set to false, i.e.

static mappings = {
  "/$controller/show/$id?"(parseRequest:false,action:'show'){
        constraints {
            // apply constraints here
        }
    }

  "/$controller/$action?/$id?"(parseRequest:true){
        constraints {
            // apply constraints here
        }
    }

(Yes, this still happens in 2.0.0 RC1)

Upvotes: 1

z.eljayyo
z.eljayyo

Reputation: 1289

Do you have an action in your controller that looks like this:

def myaction.json()

if not then you are sending data to an action that doesn't exist. if you are trying to parse JSON then use grails.converters for that matter:

import grails.converters

def jsonData = JSON.parse(params)

also this tuto might help: http://www.ibm.com/developerworks/java/library/j-grails11188/index.html

Upvotes: 0

Related Questions