cdeszaq
cdeszaq

Reputation: 31290

How does Grails' content negotiation handle opposing types?

Grails supports content negotiation from 3 different sources:

  1. Accept header
  2. Request parameter (format)
  3. URI extension

The question is, what does it do when it get content information from more than one place, especially when they don't agree with each other?

For example, what would happen if Grails recieved a request like the following:

URL: http://example.com/book/list.html?format=json
Accept: application/xml

The Accept header would resolve to xml, the URI extension would resolve to html, and the parameter would resolve to json.

What would this do:

import grails.converters.*

class BookController {

    def list() {
        def books = Book.list()
        withFormat {
            html bookList: books
            xml { render books as XML }
            json { render books as JSON }
        }
    }
}

Upvotes: 2

Views: 925

Answers (1)

John Wagenleitner
John Wagenleitner

Reputation: 11035

For Grails 2.0.0RC3 the following will return the html block.

curl -v -H "Accept: application/xml" http://localhost:8080/book/book/list.html?format=json

The order of precedence is:

  1. URI extension
  2. format request param
  3. Accept header

Note that in order to use the Accept header you must change the following parameter in the grails-app/conf/Config.groovy file (default is false):

grails.mime.use.accept.header = true

Upvotes: 5

Related Questions