Quantcast

Grails & RESTclient

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Grails & RESTclient

Pascal de Vink
I'm trying to build a REST client for my REST server (which I build
with the help of the Definitive Guide To Grails) using RESTclient.
I'm using the following code at the server side:

def save = {
       def u = new Signup()
       u.properties = params

        if (u.save()) {
            render u as XML
        } else {
            render u.errors as XML
        }
}

and this piece of code at the client:

def signup = new RESTClient(signupsUrl)

        def response = signup.post(
            contentType: XML,
            requestContentType: XML,
            body: {
                field(name:"surname", "Pascal")
                field(name:"infix", "de")
                field(name:"lastname", "Vink")
                field(name:"email", "[hidden email]")
                field(name:"signupdate", params.signupdate)
            }
        }

        return response.data

Not only does it not put anything in the database, the response.data
is virtually unreadable, at least it's not XML als I would expect.
The following is a snippet of the output of println response.data:

emailSignupfalseorg.springframework.validation.FieldErrornullableSignup.email.nullable.error.Signup.emailSignup.email.nullable.error.emailSignup.email.nullable.error.java.lang.StringSignup.email.nullable.errorsignup.email.nullable.error.Signup.emailsignup.email.nullable.error.emailsignup.email.nullable.error.java.lang.Stringsignup.email.nullable.errorSignup.email.nullable.Signup.emailSignup.email.nullable.emailSignup.email.nullable.java.lang.StringSignup.email.nullablesignup.email.nullable.Signup.emailsignup.email.nullable.emailsignup.email.nullable.java.lang.Stringsignup.email.nullablenullable.Signup.emailnullable.emailnullable.java.lang.StringnullableProperty
[{0}] of class [{1}] cannot be
nullemailSignupinfixSignupfalseorg.springframework.validation.FieldErrornullable

and this goes on for a while (full print here http://gist.github.com/152217)

Does anyone know how to build a compatible client? And perhaps a
server that returns proper XML error messages?

Thanks in advance.
Pascal de Vink

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails & RESTclient

redheat
What you are seeing in your response.data is not the XML you generated. It is already the response from the server side  to the xml request

It is the result of
render u.errors as XML

This means that parsing the XML into the new domain instance parameters has failed, which is the part u.properties = params.

Did you know that automatic parsing of REST requests has to be enabled in the Grails urlmapping as of version 1.1? See the Grails doc on REST for that.

If after that it still doesn't work you could try building the XML body not with the RESTclient markup builder but with sth else, e.g. Grails converters, from a protoype instance of the class you want to transfer.

Pascal de Vink wrote
I'm trying to build a REST client for my REST server (which I build
with the help of the Definitive Guide To Grails) using RESTclient.
I'm using the following code at the server side:

def save = {
       def u = new Signup()
       u.properties = params

        if (u.save()) {
            render u as XML
        } else {
            render u.errors as XML
        }
}

and this piece of code at the client:

def signup = new RESTClient(signupsUrl)

        def response = signup.post(
            contentType: XML,
            requestContentType: XML,
            body: {
                field(name:"surname", "Pascal")
                field(name:"infix", "de")
                field(name:"lastname", "Vink")
                field(name:"email", "pascal@example.com")
                field(name:"signupdate", params.signupdate)
            }
        }

        return response.data

Not only does it not put anything in the database, the response.data
is virtually unreadable, at least it's not XML als I would expect.
The following is a snippet of the output of println response.data:

emailSignupfalseorg.springframework.validation.FieldErrornullableSignup.email.nullable.error.Signup.emailSignup.email.nullable.error.emailSignup.email.nullable.error.java.lang.StringSignup.email.nullable.errorsignup.email.nullable.error.Signup.emailsignup.email.nullable.error.emailsignup.email.nullable.error.java.lang.Stringsignup.email.nullable.errorSignup.email.nullable.Signup.emailSignup.email.nullable.emailSignup.email.nullable.java.lang.StringSignup.email.nullablesignup.email.nullable.Signup.emailsignup.email.nullable.emailsignup.email.nullable.java.lang.Stringsignup.email.nullablenullable.Signup.emailnullable.emailnullable.java.lang.StringnullableProperty
[{0}] of class [{1}] cannot be
nullemailSignupinfixSignupfalseorg.springframework.validation.FieldErrornullable

and this goes on for a while (full print here http://gist.github.com/152217)

Does anyone know how to build a compatible client? And perhaps a
server that returns proper XML error messages?

Thanks in advance.
Pascal de Vink

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails & RESTclient

Tom Nichols
In reply to this post by Pascal de Vink
What you're seeing in the response is the toString() output from a
GPathResult instance, which is the XML stripped of all tags.
Unfortunately this is a limitation of XmlSlurper/ GPathResult that you
cannot parse the XML and then print the human-readable XML structure
from it.

If you turn on logging on the client-side as documented here:
http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging

You'll see the actual XML returned; as redheat mentioned, it looks
like you're seeing an error message response.  You could check
response.status to see if that indicates an error on the client side.
However I don't know for sure if Grails will automatically return a
non-200 HTTP status code for validation errors.  You should for a REST
interface.

-Tom



On Wed, Jul 22, 2009 at 3:38 PM, Pascal de Vink<[hidden email]> wrote:

> I'm trying to build a REST client for my REST server (which I build
> with the help of the Definitive Guide To Grails) using RESTclient.
> I'm using the following code at the server side:
>
> def save = {
>       def u = new Signup()
>       u.properties = params
>
>        if (u.save()) {
>            render u as XML
>        } else {
>            render u.errors as XML
>        }
> }
>
> and this piece of code at the client:
>
> def signup = new RESTClient(signupsUrl)
>
>        def response = signup.post(
>            contentType: XML,
>            requestContentType: XML,
>            body: {
>                field(name:"surname", "Pascal")
>                field(name:"infix", "de")
>                field(name:"lastname", "Vink")
>                field(name:"email", "[hidden email]")
>                field(name:"signupdate", params.signupdate)
>            }
>        }
>
>        return response.data
>
> Not only does it not put anything in the database, the response.data
> is virtually unreadable, at least it's not XML als I would expect.
> The following is a snippet of the output of println response.data:
>
> emailSignupfalseorg.springframework.validation.FieldErrornullableSignup.email.nullable.error.Signup.emailSignup.email.nullable.error.emailSignup.email.nullable.error.java.lang.StringSignup.email.nullable.errorsignup.email.nullable.error.Signup.emailsignup.email.nullable.error.emailsignup.email.nullable.error.java.lang.Stringsignup.email.nullable.errorSignup.email.nullable.Signup.emailSignup.email.nullable.emailSignup.email.nullable.java.lang.StringSignup.email.nullablesignup.email.nullable.Signup.emailsignup.email.nullable.emailsignup.email.nullable.java.lang.Stringsignup.email.nullablenullable.Signup.emailnullable.emailnullable.java.lang.StringnullableProperty
> [{0}] of class [{1}] cannot be
> nullemailSignupinfixSignupfalseorg.springframework.validation.FieldErrornullable
>
> and this goes on for a while (full print here http://gist.github.com/152217)
>
> Does anyone know how to build a compatible client? And perhaps a
> server that returns proper XML error messages?
>
> Thanks in advance.
> Pascal de Vink
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails & RESTclient

John Fletcher-9
In reply to this post by Pascal de Vink
Pascal,

Your question inspired me to write up my recent experiences building a
RESTful client for my Grails web service... I hope it is helpful:
http://www.saltwebsites.com/blog/java-client-restful-grails-web-service.

Regards,
John

-------- Mensaje original --------

I'm trying to build a REST client for my REST server (which I build
with the help of the Definitive Guide To Grails) using RESTclient.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails & RESTclient

Tom Nichols
Hi John,

I tried to post a reply on your blog but the Captcha kept failing with
the message "unknown csid."  Anyway, here is my reply:

Thanks for the helpful hints.  I agree with the lack of good
documentation; it is partly due to the lack of good public REST
services to use as examples (or a lack of my awareness of them).  My
Twitter example
(http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/main/script/twitter_restbuilder.groovy?r=root)
provides a good example of using Twitter's REST interface.
Unfortunately, as you alluded, these services use URL query
parameters, not a request body for content creation (i.e. PUTs and
POSTs).

Here's an example that should accomplish your example on your blog:
<code>
def client = new RESTClient( uri:'http://localhost:8080/myapp/',
                contentType: 'text/xml' )

client.post( path : 'machine/saveXml/3',
        body : {
                machine {
                        model( enumType: 'MachineModel', 'Forklift' )
                        name 'Big lifter'
                }
        } )
</code>

If you want to send and receive raw XML you can do it like this:
<code>
def client = new RESTClient( uri:'http://localhost:8080/myapp/',
                contentType: ContentType.TEXT,
                headers: ['Content-Type':'text/xml', Accept:'text/xml'] )

def resp = client.post( path : 'machine/saveXml/3', body : myXml )
println resp.data.text
</code>

There may be some issue with response buffering (the RESTClient
assumes the response stream is closed) but that will be fixed in the
next RC release.  Hope this is helpful.  Generally if you only want to
see the XML text for debugging purposes, it's probably easiest to turn
on debug logging for httpclient, which will show you the request and
response body (as well as headers) and can be switched off without
modifying your code.


-Tom


2009/7/24 John Fletcher <[hidden email]>:

> Pascal,
>
> Your question inspired me to write up my recent experiences building a RESTful client for my Grails web service... I hope it is helpful: http://www.saltwebsites.com/blog/java-client-restful-grails-web-service.
>
> Regards,
> John
>
> -------- Mensaje original --------
>
> I'm trying to build a REST client for my REST server (which I build
> with the help of the Definitive Guide To Grails) using RESTclient.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...