Marc Palmer | 9 May 17:06
Picon
Favicon

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


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


Gmane