ZnEasy and post:data:
Hey all, This is probably me missing something, but: for an upcoming presentation, I wanted to find the tersest way to demonstrate making an HTTP POST. Something equivalent to Python's requests.post('http://some/thing', data={'foo': 'bar', 'baz': 'quux'}) The closest I could come up with for Pharo was ZnEasy post: 'http://some/thing' data: (ZnApplicationFormUrlEncodedEntity withAll: { 'foo' -> 'bar'. 'baz' -> 'quux'. } asDictionary) I just wanted to verify that is, in fact, the shortest way to do this. I feel as if I'm almost certainly missing a utility method around the ZnApplicationFormUrlEncodedEntity bit, and, especially for demos, terseness matters. (It's obviously trivial to write a utility method if this *is* the shortest way to do this, but then people in the audience won't be able to execute the resulting code snippet without the utility method.) Thanks, --Benjamin
ZnClient is your friend (it is a builder) : ZnClient new url: 'http://some/thing'; formAt: #foo put: #bar; formAt: 'baz' put: 'quux'; post. HTH, Sven PS: Check out the 'accessing request' protocol for more ways to construct your request. Also, the response is kept in the object: #post returns something called the contents, but you can add ; response or ; isSuccess at the end. Check out the class comment or unit tests. On 02 Oct 2014, at 02:17, Benjamin Pollack <benjamin@bitquabit.com> wrote:
Hey all,
This is probably me missing something, but: for an upcoming presentation, I wanted to find the tersest way to demonstrate making an HTTP POST. Something equivalent to Python's
requests.post('http://some/thing', data={'foo': 'bar', 'baz': 'quux'})
The closest I could come up with for Pharo was
ZnEasy post: 'http://some/thing' data: (ZnApplicationFormUrlEncodedEntity withAll: { 'foo' -> 'bar'. 'baz' -> 'quux'. } asDictionary)
I just wanted to verify that is, in fact, the shortest way to do this. I feel as if I'm almost certainly missing a utility method around the ZnApplicationFormUrlEncodedEntity bit, and, especially for demos, terseness matters. (It's obviously trivial to write a utility method if this *is* the shortest way to do this, but then people in the audience won't be able to execute the resulting code snippet without the utility method.)
Thanks,
--Benjamin
On Thu, Oct 2, 2014, at 01:28 AM, Sven Van Caekenberghe wrote:
ZnClient is your friend (it is a builder) :
ZnClient new url: 'http://some/thing'; formAt: #foo put: #bar; formAt: 'baz' put: 'quux'; post.
Ah, that's indeed perfect. I think I discounted ZnClient initially because I was thinking of it being primarily useful for pipelined operations, but your proposal is *way* cleaner, even if I throw beOneShot in there. Thanks for the pointer. --Benjamin
participants (2)
-
Benjamin Pollack -
Sven Van Caekenberghe