Al-Punk
Al-Punk

Reputation: 3659

Rendering a view with custom mime type in Grails

I am attempting to render text for reponse in Grails with a custom contentType. My desired contentType is: application/vnd.api+json

I am testing with the following

render(contentType: "application/vnd.api+json") {
    message = 'some text'
    foo = 'bar'
}

which does not render throwing an exception that message is a missing property.

While the following works fine:

render(contentType: "text/json") {
    message = 'some text'
    foo = 'bar'
}

My Config.groovy has the following under json mime.type:

grails.mime.types = [
...
    json:          [
        'application/json',
        'text/json',
        'application/vnd.api+json'
    ],
...
]

My question, how to render with a custom mime-type in Grails?

Upvotes: 2

Views: 2555

Answers (1)

dmahapatro
dmahapatro

Reputation: 50265

Do you have the accept header in the request set the custom content-type?

The accept header is one way for client to inform server which content-type would be acceptable for itself.

In config.groovy the below setting has to be set as well to use accept headers

grails.mime.use.accept.header = true

I would also try rendering the response in the conventional way:

render(contentType: "application/vnd.api+json", text: [message: 'some text', foo: 'bar'])

Upvotes: 4

Related Questions