user2586917
user2586917

Reputation: 774

How to specify content-type using the Grails REST client builder plugin?

I'm using the Grails REST client builder plugin (https://github.com/grails-plugins/grails-rest-client-builder) like so:

final def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
final def resp = rest.get(uri) 

So the server always returns the result as XML - how do I (the client) specify that I want the result as JSON?

Upvotes: 0

Views: 1985

Answers (2)

jamesj74
jamesj74

Reputation: 13

Try like this:

final def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
final def resp = rest.get(uri) {
  accept "application/json"  # Add this bit
}

Upvotes: 1

Joshua Moore
Joshua Moore

Reputation: 24776

Setting the content type is done through the contentType method. The documetnation points this out with the following example.

        def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){
        auth System.getProperty("artifactory.user"), System.getProperty("artifactory.pass")
        contentType "application/vnd.org.jfrog.artifactory.security.Group+json"
        json {
            name = "test-group"
            description = "A temporary test group"
        }
    }

Upvotes: 2

Related Questions