raffian
raffian

Reputation: 32056

Easy way to render JSON with HTTP status code in Grails

Is there a shorthand way to do this without explicit "text/json" designation?

def remoteError = {     
  render( status: 500, contentType: "text/json"){
      error( exception: "a remote exception occurred")
  }
}

I tried using as JSON...no content is returned but the status code is correct...

render( status: 500, exception: params.exception) as JSON 

Upvotes: 23

Views: 15833

Answers (2)

Cagatay Kalan
Cagatay Kalan

Reputation: 4126

render(status:500,text:(errors as JSON).toString(),contentType: 'application/json')

Upvotes: 3

krock
krock

Reputation: 29619

If you use a converter parameter to render then you cannot specify any other parameter such as status like you normally would when using gsp views. You can however set the response status prior to calling render:

response.status = 500
render([error: 'an error occurred'] as JSON)

Upvotes: 45

Related Questions