Quantcast

HTTP Builder Post Example

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

HTTP Builder Post Example

Ronald R. DiFrango-2
All,

Are there any examples of HTTP Builder sending post data such as JSON
or FORM data?  Is this even possible?

Thanks,

Ron

---------------------------------------------------------------------
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: HTTP Builder Post Example

Thom Nichols
Yup, perfectly possible.  Here is an example:


http.post( 'some/url', JSON ) {
  body = someObject
  response.success { resp, json ->
    // response will be parsed as JSON as well..
  }
}


Sorry, I realized there isn't an example of this on the website-- I thought there was.

By default, whatever is assigned in setBody() is encoded using the given request content-type (in this case, JSON).  To see how exactly this is going to be handled (because each content-type encoder can handle different types of data) you'll want to look at EncoderRegistry -- in this case, encodeJSON:
http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
So body can be a closure, a map, or bean-style object.

See also:  http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
and:  http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)

I suspect I should improve the documentation on EncoderRegistry as well :)  I'll put an example in the site documentation too.

-Tom


2009/3/13 Ronald R. DiFrango <[hidden email]>
All,

Are there any examples of HTTP Builder sending post data such as JSON
or FORM data?  Is this even possible?

Thanks,

Ron

---------------------------------------------------------------------
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: HTTP Builder Post Example

Ronald R. DiFrango-2
Tom,

Thanks that is helpful.  Yeah having a simple example of sending data
via the body would be great.  Can you also Post form data like you
with HTTPClient?  I know that is a little bit more involved having
done it on the Java side.

Thanks,

Ron

On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]> wrote:

> Yup, perfectly possible.  Here is an example:
>
>
> http.post( 'some/url', JSON ) {
>   body = someObject
>   response.success { resp, json ->
>     // response will be parsed as JSON as well..
>   }
> }
>
>
> Sorry, I realized there isn't an example of this on the website-- I thought
> there was.
>
> By default, whatever is assigned in setBody() is encoded using the given
> request content-type (in this case, JSON).  To see how exactly this is going
> to be handled (because each content-type encoder can handle different types
> of data) you'll want to look at EncoderRegistry -- in this case, encodeJSON:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
> So body can be a closure, a map, or bean-style object.
>
> See also:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
> and:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>
> I suspect I should improve the documentation on EncoderRegistry as well :)
> I'll put an example in the site documentation too.
>
> -Tom
>
>
> 2009/3/13 Ronald R. DiFrango <[hidden email]>
>>
>> All,
>>
>> Are there any examples of HTTP Builder sending post data such as JSON
>> or FORM data?  Is this even possible?
>>
>> Thanks,
>>
>> Ron
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>



--
Ron DiFrango
http://rdifrango.blogspot.com/

---------------------------------------------------------------------
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: HTTP Builder Post Example

Thom Nichols
Nope, doing an HTML form post is just as easy.  This is the case where you'll want to set the requestContentType to ContentType.URLENC (or the string "application/x-www-form-urlencoded").  The corresponding Encoder can then accept a map which will be converted to a POST query string.

Here is a full example from the unit tests that uses Twitter:
        def http = new HTTPBuilder('http://twitter.com/statuses/')
        // set authentication params .....
       
        def msg = "HTTPBuilder unit test was run on ${new Date()}"
       
        http.request( POST, XML ) { req ->
            uri.path = 'update.xml'
            send URLENC, [status:msg,source:'httpbuilder']
           
            //req.params.setBooleanParameter 'http.protocol.expect-continue', false
           
             response.success = { resp, xml ->
                println "Tweet response status: ${resp.statusLine}"
                assert resp.statusLine.statusCode == 200
                assert xml instanceof GPathResult
               
                assert xml.text == msg
                assert xml.user.screen_name == twitter.user
                return xml.id
            }
        }


So this a POST, expecting an XML response.  THen in the request configuration we're saying to send( contentType, data ), which is a convenience method for
requestContentType = ContentType.URLENC
body = // whatever data

You can see the full example in the unit test:
http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy?r=root:#l108

I guess I didn't realize the site documentation doesn't include any POST examples -- that leaves out a big portion of what HTTPBuilder is meant to do. 


-Tom


On Sat, Mar 14, 2009 at 9:44 AM, Ronald R. DiFrango <[hidden email]> wrote:
Tom,

Thanks that is helpful.  Yeah having a simple example of sending data
via the body would be great.  Can you also Post form data like you
with HTTPClient?  I know that is a little bit more involved having
done it on the Java side.

Thanks,

Ron

On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]> wrote:
> Yup, perfectly possible.  Here is an example:
>
>
> http.post( 'some/url', JSON ) {
>   body = someObject
>   response.success { resp, json ->
>     // response will be parsed as JSON as well..
>   }
> }
>
>
> Sorry, I realized there isn't an example of this on the website-- I thought
> there was.
>
> By default, whatever is assigned in setBody() is encoded using the given
> request content-type (in this case, JSON).  To see how exactly this is going
> to be handled (because each content-type encoder can handle different types
> of data) you'll want to look at EncoderRegistry -- in this case, encodeJSON:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
> So body can be a closure, a map, or bean-style object.
>
> See also:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
> and:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>
> I suspect I should improve the documentation on EncoderRegistry as well :)
> I'll put an example in the site documentation too.
>
> -Tom
>
>
> 2009/3/13 Ronald R. DiFrango <[hidden email]>
>>
>> All,
>>
>> Are there any examples of HTTP Builder sending post data such as JSON
>> or FORM data?  Is this even possible?
>>
>> Thanks,
>>
>> Ron
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>



--
Ron DiFrango
http://rdifrango.blogspot.com/

---------------------------------------------------------------------
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: HTTP Builder Post Example

Ronald R. DiFrango-2
Tom,

Thanks, those examples on the site would be useful or I could blog
about it ;-)  It would be helpful if the examples include the the
imports necessary because I seem to be constantly fighting that battle
as well.

Ron

On Sat, Mar 14, 2009 at 2:28 PM, Tom Nichols <[hidden email]> wrote:

> Nope, doing an HTML form post is just as easy.  This is the case where
> you'll want to set the requestContentType to ContentType.URLENC (or the
> string "application/x-www-form-urlencoded").  The corresponding Encoder can
> then accept a map which will be converted to a POST query string.
>
> Here is a full example from the unit tests that uses Twitter:
>         def http = new HTTPBuilder('http://twitter.com/statuses/')
>         // set authentication params .....
>
>         def msg = "HTTPBuilder unit test was run on ${new Date()}"
>
>         http.request( POST, XML ) { req ->
>             uri.path = 'update.xml'
>             send URLENC, [status:msg,source:'httpbuilder']
>
>             //req.params.setBooleanParameter
> 'http.protocol.expect-continue', false
>
>              response.success = { resp, xml ->
>                 println "Tweet response status: ${resp.statusLine}"
>                 assert resp.statusLine.statusCode == 200
>                 assert xml instanceof GPathResult
>
>                 assert xml.text == msg
>                 assert xml.user.screen_name == twitter.user
>                 return xml.id
>             }
>         }
>
>
> So this a POST, expecting an XML response.  THen in the request
> configuration we're saying to send( contentType, data ), which is a
> convenience method for
> requestContentType = ContentType.URLENC
> body = // whatever data
>
> You can see the full example in the unit test:
> http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy?r=root:#l108
>
> I guess I didn't realize the site documentation doesn't include any POST
> examples -- that leaves out a big portion of what HTTPBuilder is meant to
> do.
>
>
> -Tom
>
>
> On Sat, Mar 14, 2009 at 9:44 AM, Ronald R. DiFrango <[hidden email]>
> wrote:
>>
>> Tom,
>>
>> Thanks that is helpful.  Yeah having a simple example of sending data
>> via the body would be great.  Can you also Post form data like you
>> with HTTPClient?  I know that is a little bit more involved having
>> done it on the Java side.
>>
>> Thanks,
>>
>> Ron
>>
>> On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]> wrote:
>> > Yup, perfectly possible.  Here is an example:
>> >
>> >
>> > http.post( 'some/url', JSON ) {
>> >   body = someObject
>> >   response.success { resp, json ->
>> >     // response will be parsed as JSON as well..
>> >   }
>> > }
>> >
>> >
>> > Sorry, I realized there isn't an example of this on the website-- I
>> > thought
>> > there was.
>> >
>> > By default, whatever is assigned in setBody() is encoded using the given
>> > request content-type (in this case, JSON).  To see how exactly this is
>> > going
>> > to be handled (because each content-type encoder can handle different
>> > types
>> > of data) you'll want to look at EncoderRegistry -- in this case,
>> > encodeJSON:
>> >
>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
>> > So body can be a closure, a map, or bean-style object.
>> >
>> > See also:
>> >
>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
>> > and:
>> >
>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>> >
>> > I suspect I should improve the documentation on EncoderRegistry as well
>> > :)
>> > I'll put an example in the site documentation too.
>> >
>> > -Tom
>> >
>> >
>> > 2009/3/13 Ronald R. DiFrango <[hidden email]>
>> >>
>> >> All,
>> >>
>> >> Are there any examples of HTTP Builder sending post data such as JSON
>> >> or FORM data?  Is this even possible?
>> >>
>> >> Thanks,
>> >>
>> >> Ron
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe from this list, please visit:
>> >>
>> >>    http://xircles.codehaus.org/manage_email
>> >>
>> >>
>> >
>> >
>>
>>
>>
>> --
>> Ron DiFrango
>> http://rdifrango.blogspot.com/
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>



--
Ron DiFrango
http://rdifrango.blogspot.com/

---------------------------------------------------------------------
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: HTTP Builder Post Example

Thom Nichols
On Mon, Mar 16, 2009 at 11:41 AM, Ronald R. DiFrango
<[hidden email]> wrote:
> It would be helpful if the examples include the the
> imports necessary because I seem to be constantly fighting that battle
> as well.

No prob.  I'll make sure my updated documentation includes the import
statements.

---------------------------------------------------------------------
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: HTTP Builder Post Example

Ronald R. DiFrango-2
In reply to this post by Thom Nichols
One more thing on this one, it got me thinking about how do I name the form or make them form fields?  The method below does not does not appear to do it correctly.  Here is what we do on the Java side:

Part[] parts = { new StringPart("noderef", id),

new StringPart("impersonate", username), new StringPart("title", ""),

new StringPart("desc", ""), new FilePart("file", id, tmpFile) };


Is this how HttpBuilder does it?


On Sat, Mar 14, 2009 at 2:28 PM, Tom Nichols <[hidden email]> wrote:
Nope, doing an HTML form post is just as easy.  This is the case where you'll want to set the requestContentType to ContentType.URLENC (or the string "application/x-www-form-urlencoded").  The corresponding Encoder can then accept a map which will be converted to a POST query string.

Here is a full example from the unit tests that uses Twitter:
        def http = new HTTPBuilder('http://twitter.com/statuses/')
        // set authentication params .....
       
        def msg = "HTTPBuilder unit test was run on ${new Date()}"
       
        http.request( POST, XML ) { req ->
            uri.path = 'update.xml'
            send URLENC, [status:msg,source:'httpbuilder']
           
            //req.params.setBooleanParameter 'http.protocol.expect-continue', false
           
             response.success = { resp, xml ->
                println "Tweet response status: ${resp.statusLine}"
                assert resp.statusLine.statusCode == 200
                assert xml instanceof GPathResult
               
                assert xml.text == msg
                assert xml.user.screen_name == twitter.user
                return xml.id
            }
        }


So this a POST, expecting an XML response.  THen in the request configuration we're saying to send( contentType, data ), which is a convenience method for
requestContentType = ContentType.URLENC
body = // whatever data

You can see the full example in the unit test:
http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy?r=root:#l108

I guess I didn't realize the site documentation doesn't include any POST examples -- that leaves out a big portion of what HTTPBuilder is meant to do. 


-Tom



On Sat, Mar 14, 2009 at 9:44 AM, Ronald R. DiFrango <[hidden email]> wrote:
Tom,

Thanks that is helpful.  Yeah having a simple example of sending data
via the body would be great.  Can you also Post form data like you
with HTTPClient?  I know that is a little bit more involved having
done it on the Java side.

Thanks,

Ron

On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]> wrote:
> Yup, perfectly possible.  Here is an example:
>
>
> http.post( 'some/url', JSON ) {
>   body = someObject
>   response.success { resp, json ->
>     // response will be parsed as JSON as well..
>   }
> }
>
>
> Sorry, I realized there isn't an example of this on the website-- I thought
> there was.
>
> By default, whatever is assigned in setBody() is encoded using the given
> request content-type (in this case, JSON).  To see how exactly this is going
> to be handled (because each content-type encoder can handle different types
> of data) you'll want to look at EncoderRegistry -- in this case, encodeJSON:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
> So body can be a closure, a map, or bean-style object.
>
> See also:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
> and:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>
> I suspect I should improve the documentation on EncoderRegistry as well :)
> I'll put an example in the site documentation too.
>
> -Tom
>
>
> 2009/3/13 Ronald R. DiFrango <[hidden email]>
>>
>> All,
>>
>> Are there any examples of HTTP Builder sending post data such as JSON
>> or FORM data?  Is this even possible?
>>
>> Thanks,
>>
>> Ron
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>



--
Ron DiFrango
http://rdifrango.blogspot.com/

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

   http://xircles.codehaus.org/manage_email






--
Ron DiFrango
http://rdifrango.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: HTTP Builder Post Example

Ronald R. DiFrango-2
I forgot one more bit:

method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));


On Mon, Mar 16, 2009 at 2:53 PM, Ronald R. DiFrango <[hidden email]> wrote:
One more thing on this one, it got me thinking about how do I name the form or make them form fields?  The method below does not does not appear to do it correctly.  Here is what we do on the Java side:

Part[] parts = { new StringPart("noderef", id),

new StringPart("impersonate", username), new StringPart("title", ""),

new StringPart("desc", ""), new FilePart("file", id, tmpFile) };


Is this how HttpBuilder does it?


On Sat, Mar 14, 2009 at 2:28 PM, Tom Nichols <[hidden email]> wrote:
Nope, doing an HTML form post is just as easy.  This is the case where you'll want to set the requestContentType to ContentType.URLENC (or the string "application/x-www-form-urlencoded").  The corresponding Encoder can then accept a map which will be converted to a POST query string.

Here is a full example from the unit tests that uses Twitter:
        def http = new HTTPBuilder('http://twitter.com/statuses/')
        // set authentication params .....
       
        def msg = "HTTPBuilder unit test was run on ${new Date()}"
       
        http.request( POST, XML ) { req ->
            uri.path = 'update.xml'
            send URLENC, [status:msg,source:'httpbuilder']
           
            //req.params.setBooleanParameter 'http.protocol.expect-continue', false
           
             response.success = { resp, xml ->
                println "Tweet response status: ${resp.statusLine}"
                assert resp.statusLine.statusCode == 200
                assert xml instanceof GPathResult
               
                assert xml.text == msg
                assert xml.user.screen_name == twitter.user
                return xml.id
            }
        }


So this a POST, expecting an XML response.  THen in the request configuration we're saying to send( contentType, data ), which is a convenience method for
requestContentType = ContentType.URLENC
body = // whatever data

You can see the full example in the unit test:
http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy?r=root:#l108

I guess I didn't realize the site documentation doesn't include any POST examples -- that leaves out a big portion of what HTTPBuilder is meant to do. 


-Tom



On Sat, Mar 14, 2009 at 9:44 AM, Ronald R. DiFrango <[hidden email]> wrote:
Tom,

Thanks that is helpful.  Yeah having a simple example of sending data
via the body would be great.  Can you also Post form data like you
with HTTPClient?  I know that is a little bit more involved having
done it on the Java side.

Thanks,

Ron

On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]> wrote:
> Yup, perfectly possible.  Here is an example:
>
>
> http.post( 'some/url', JSON ) {
>   body = someObject
>   response.success { resp, json ->
>     // response will be parsed as JSON as well..
>   }
> }
>
>
> Sorry, I realized there isn't an example of this on the website-- I thought
> there was.
>
> By default, whatever is assigned in setBody() is encoded using the given
> request content-type (in this case, JSON).  To see how exactly this is going
> to be handled (because each content-type encoder can handle different types
> of data) you'll want to look at EncoderRegistry -- in this case, encodeJSON:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
> So body can be a closure, a map, or bean-style object.
>
> See also:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
> and:
> http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>
> I suspect I should improve the documentation on EncoderRegistry as well :)
> I'll put an example in the site documentation too.
>
> -Tom
>
>
> 2009/3/13 Ronald R. DiFrango <[hidden email]>
>>
>> All,
>>
>> Are there any examples of HTTP Builder sending post data such as JSON
>> or FORM data?  Is this even possible?
>>
>> Thanks,
>>
>> Ron
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>



--
Ron DiFrango
http://rdifrango.blogspot.com/

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

   http://xircles.codehaus.org/manage_email






--



--
Ron DiFrango
http://rdifrango.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: HTTP Builder Post Example

Thom Nichols
There's no special support for multipart request body encoding at the
moment (part of the reason why you won't find a "multipart" value on
the groovyx.net.http.ContentType enum.

But since HTTPBuilder is built directly on top of HttpClient, you
should be able to do this (at least, if your example works with
HttpClient 4.0).

Try this for your request:

http.request( POST, 'whatever-response-content-type' ) { request ->

  requestContentType = 'multipart/form-data'

  def parts = // same as your Java code
  request.requestEntity = new MultipartRequestEntity( parts, request.params )

  // any response handling code
}

The single argument passed to the request config closure is the
request method instance that will be used to send the request.  So you
can configure your request and handle the response within the closure,
and be rest assured that the associated request and response streams
will be cleaned up after the closure has executed.  (i.e. you don't
need a finally { resp.consumeContent(); } block.

Note that HTTPBuilder uses HttpClient 4.0 so if your Java HttpClient
code uses HC 3x then you may need to change slightly if HC 4's
multipart request entity is different.  But you should find good
examples of that in the HttpClient documentation.


-Tom


On Mon, Mar 16, 2009 at 2:56 PM, Ronald R. DiFrango
<[hidden email]> wrote:

> I forgot one more bit:
>
> method.setRequestEntity(new MultipartRequestEntity(parts,
> method.getParams()));
>
> On Mon, Mar 16, 2009 at 2:53 PM, Ronald R. DiFrango <[hidden email]>
> wrote:
>>
>> One more thing on this one, it got me thinking about how do I name the
>> form or make them form fields?  The method below does not does not appear to
>> do it correctly.  Here is what we do on the Java side:
>>
>> Part[] parts = { new StringPart("noderef", id),
>>
>> new StringPart("impersonate", username), new StringPart("title", ""),
>>
>> new StringPart("desc", ""), new FilePart("file", id, tmpFile) };
>>
>> Is this how HttpBuilder does it?
>>
>> On Sat, Mar 14, 2009 at 2:28 PM, Tom Nichols <[hidden email]> wrote:
>>>
>>> Nope, doing an HTML form post is just as easy.  This is the case where
>>> you'll want to set the requestContentType to ContentType.URLENC (or the
>>> string "application/x-www-form-urlencoded").  The corresponding Encoder can
>>> then accept a map which will be converted to a POST query string.
>>>
>>> Here is a full example from the unit tests that uses Twitter:
>>>         def http = new HTTPBuilder('http://twitter.com/statuses/')
>>>         // set authentication params .....
>>>
>>>         def msg = "HTTPBuilder unit test was run on ${new Date()}"
>>>
>>>         http.request( POST, XML ) { req ->
>>>             uri.path = 'update.xml'
>>>             send URLENC, [status:msg,source:'httpbuilder']
>>>
>>>             //req.params.setBooleanParameter
>>> 'http.protocol.expect-continue', false
>>>
>>>              response.success = { resp, xml ->
>>>                 println "Tweet response status: ${resp.statusLine}"
>>>                 assert resp.statusLine.statusCode == 200
>>>                 assert xml instanceof GPathResult
>>>
>>>                 assert xml.text == msg
>>>                 assert xml.user.screen_name == twitter.user
>>>                 return xml.id
>>>             }
>>>         }
>>>
>>>
>>> So this a POST, expecting an XML response.  THen in the request
>>> configuration we're saying to send( contentType, data ), which is a
>>> convenience method for
>>> requestContentType = ContentType.URLENC
>>> body = // whatever data
>>>
>>> You can see the full example in the unit test:
>>>
>>> http://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy?r=root:#l108
>>>
>>> I guess I didn't realize the site documentation doesn't include any POST
>>> examples -- that leaves out a big portion of what HTTPBuilder is meant to
>>> do.
>>>
>>>
>>> -Tom
>>>
>>>
>>> On Sat, Mar 14, 2009 at 9:44 AM, Ronald R. DiFrango
>>> <[hidden email]> wrote:
>>>>
>>>> Tom,
>>>>
>>>> Thanks that is helpful.  Yeah having a simple example of sending data
>>>> via the body would be great.  Can you also Post form data like you
>>>> with HTTPClient?  I know that is a little bit more involved having
>>>> done it on the Java side.
>>>>
>>>> Thanks,
>>>>
>>>> Ron
>>>>
>>>> On Fri, Mar 13, 2009 at 7:04 PM, Tom Nichols <[hidden email]>
>>>> wrote:
>>>> > Yup, perfectly possible.  Here is an example:
>>>> >
>>>> >
>>>> > http.post( 'some/url', JSON ) {
>>>> >   body = someObject
>>>> >   response.success { resp, json ->
>>>> >     // response will be parsed as JSON as well..
>>>> >   }
>>>> > }
>>>> >
>>>> >
>>>> > Sorry, I realized there isn't an example of this on the website-- I
>>>> > thought
>>>> > there was.
>>>> >
>>>> > By default, whatever is assigned in setBody() is encoded using the
>>>> > given
>>>> > request content-type (in this case, JSON).  To see how exactly this is
>>>> > going
>>>> > to be handled (because each content-type encoder can handle different
>>>> > types
>>>> > of data) you'll want to look at EncoderRegistry -- in this case,
>>>> > encodeJSON:
>>>> >
>>>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/EncoderRegistry.html#encodeJSON(java.lang.Object)
>>>> > So body can be a closure, a map, or bean-style object.
>>>> >
>>>> > See also:
>>>> >
>>>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#setBody(java.lang.Object)
>>>> > and:
>>>> >
>>>> > http://groovy.codehaus.org/modules/http-builder/apidocs/groovyx/net/http/HTTPBuilder.RequestConfigDelegate.html#send(java.lang.Object,%20java.lang.Object)
>>>> >
>>>> > I suspect I should improve the documentation on EncoderRegistry as
>>>> > well :)
>>>> > I'll put an example in the site documentation too.
>>>> >
>>>> > -Tom
>>>> >
>>>> >
>>>> > 2009/3/13 Ronald R. DiFrango <[hidden email]>
>>>> >>
>>>> >> All,
>>>> >>
>>>> >> Are there any examples of HTTP Builder sending post data such as JSON
>>>> >> or FORM data?  Is this even possible?
>>>> >>
>>>> >> Thanks,
>>>> >>
>>>> >> Ron
>>>> >>
>>>> >> ---------------------------------------------------------------------
>>>> >> To unsubscribe from this list, please visit:
>>>> >>
>>>> >>    http://xircles.codehaus.org/manage_email
>>>> >>
>>>> >>
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> Ron DiFrango
>>>> http://rdifrango.blogspot.com/
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>
>>
>>
>> --
>> Ron DiFrango
>> http://rdifrango.blogspot.com/
>
>
>
> --
> Ron DiFrango
> http://rdifrango.blogspot.com/
>

---------------------------------------------------------------------
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: HTTP Builder Post Example

Thom Nichols
2009/3/16 Tom Nichols <[hidden email]>:
> The single argument passed to the request config closure is the
> request method instance that will be used to send the request.

Hi Ron,

I realized I mis-spoke in my example i gave you.  The argument passed
to the request configuration closure is _not_ currently the request
method instance; instead it is the client instance.  This is a little
counter-intuitive and not nearly as useful for a number of reasons --

1.  Passing the client to the request config closure implies that any
settings in that closure apply only to that single request.  If you
call methods on the client which is passed into that closure, those
changes persist (since the HTTPBuilder instance re-uses a single
BasicHttpClient instance internally.)
2.  Not only that, but there is no other way for the user to access
the request method instance, which should be available for cases like
yours where you're interested in doing direct HttpClient operations
that don't have a wrapper API in HTTPBuilder.

So really, the request method _should_ be passed in to the closure,
but it is _not_ currently.  The client instance is already available
as a property on HTTPClient, so it is redundant to pass it as a
parameter to the closure anyways.  So although this is a breaking
change, I don't believe I have any documentation guaranteeing this
behavior, so I don't feel so bad making a breaking change -- anyway,
this is the reason why HTTPBuilder is pre-1.0.  :)  Although I really
don't like making potentially dramatic changes like this, I think
you'll agree that it's a good step.  Thanks for the patience, and I
promise to have it all properly documented soon :)

As always, input & feedback are appreciated.

-Tom

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

    http://xircles.codehaus.org/manage_email


Loading...