Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to render JSON with HTTP status code in Grails

Tags:

grails

groovy

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 
like image 864
raffian Avatar asked May 23 '12 19:05

raffian


2 Answers

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)
like image 141
krock Avatar answered Nov 02 '22 04:11

krock


render(status:500,text:(errors as JSON).toString(),contentType: 'application/json')
like image 34
Cagatay Kalan Avatar answered Nov 02 '22 05:11

Cagatay Kalan