Ed Clark | 9 May 17:36
Favicon

Re: [groovy-user] RFE: Add withWriter and withOutputStream to URL/URLConnection

Marc,

Looks interesting to me.

On the "response" part, it may be nice to have a way to
specify the handling of the different final response codes:
  - being able to retry on (some) 5xx responses (after
     some specified delay),
  - being able to select the "best" next hop on a 3xx
     response that has a redirection list,
  - error handling of 4xx or 6xx responses (e.g., using an
     alternate URL),
  - maybe even being able to say how to handle login
     challenges (tho' I'm not to sure how easy it is to detect
     those)

Ed Clark

Marc Palmer wrote:
>
> On 9 May 2008, at 15:28, Marc Palmer wrote:
>
>> Hi,
>>
>> Using URL to POST is not really made any easier by the GDK it seems.
>>
>> How about we add withWriter/withOutputStream to URL/URLConnection?
>>
>> We could probably put them on URL and make it set the http method to 
>> POST on the url connection internally.
>>
>
> Actually, how would people feel about a HttpRequestBuilder?
>
> It could do stuff like this:
>
> def url = new URL("http://someserver.com/rest_api/dosomething")
> def req = new HttpRequestBuilder(url)
>
> req.post {
>     headers {
>         "Content-Type" "application/xml"
>         Host "someserver.com"
>         "User-Agent" "Groovy request builder 1.0"
>     }
>
>     // body("Something inline here")
>     // -or- render using markupbuilder
>     body {
>         write '<?xml version="1.0" encoding="UTF-8"?>'
>
>         someRoot {
>             someNodeA("The value here")
>             someNodeB("The value here")
>         }
>     }
>     // -or- set url request params, automatically 
> application/x-url-form or whatever it is encoded
>     //params {
>     //    userId = "10494894"
>     //    authToken = "DFJKHJHEFKJHFKJHEFKJHFK"
>     //}
>
>     // read response with either Reader or InputStream depending on 
> arg type in closure
>     withResponse { InputStream stream ->
>         assert headers.'Content-Type' == "application/xml"
>         assert status == 200 // delegate to an object with access to 
> headers.*/status etc
>
>         ... read the response reader/stream
>     }
> }
>
> -or a GET -
>
> req.get {
>     headers {
>         "Content-Type" "application/xml"
>         Host "someserver.com"
>         "User-Agent" "Groovy request builder 1.0"
>     }
>
>     // Set url request params, automatically URL encoded
>     params {
>         userId = "10494894"
>         authToken = "DFJKHJHEFKJHFKJHEFKJHFK"
>     }
>
>     withResponse { reader ->
>         assert status == 200
>
>         ... read the respone
>     }
> }
>
> I think this would be pretty sweet for implementing HTTP code...
>
> Marc
>
>
> ---------------------------------------------------------------------
> 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


Gmane