Scott
Scott

Reputation: 17047

Change Content-Type of JSON response while using 'as JSON' to render response

It seems as if in Grails, the default contentType used to render a JSON response when using something like what follows:

render Book.list(params) as JSON

is application/json

There is of course a more longhanded way to define the content type:

render(contentType:"text/json") {
    book(title:b.title,author:b.author)
}

Is there a way to use the shorthand way, and still get a response content-type of "text/json"?

Note: examples taken from here

Upvotes: 1

Views: 5867

Answers (1)

gotomanners
gotomanners

Reputation: 7926

In your grails config.groovy you can set the following mime type properties.

grails.mime.types = [ html: ['text/html','application/xhtml+xml'],
                xml: ['text/xml', 'application/xml'],
                text: 'text/plain',
                js: 'text/javascript',
                rss: 'application/rss+xml',
                atom: 'application/atom+xml',
                css: 'text/css',
                csv: 'text/csv',
                all: '*/*',
                json: ['application/json','text/json'],
                form: 'application/x-www-form-urlencoded',
                multipartForm: 'multipart/form-data'
]

Try changing the order of the list of values for the json property to ['text/json','application/json']

Upvotes: 4

Related Questions