Youâre (one of) my heroes ! Thanks Sven a lot for all your great contributions ! Cédrick Side note: perfect late binding information for the lecture I do tomorrow with Pharo where I show students client server interaction with ZnClient, ZnServer ⦠^_^
Le 7 nov. 2018 à 17:36, Sven Van Caekenberghe <sven@stfx.eu> a écrit :
Hi,
I added a new convenience method to Zinc HTTP Components: ZnClient>>forJsonREST. This configures a ZnClient (HTTP client) to talk to standard JSON REST web services. Here are a couple of examples:
ZnClient new forJsonREST; get: 'https://jsonplaceholder.typicode.com/users'.
What #forJsonREST does is 3 things: set the 'Accept' header to 'application/json', install a #contentReader that parses incoming JSON as well as a #contentWriter that generates JSON.
ZnClient new systemPolicy; forJsonREST; url: 'https://jsonplaceholder.typicode.com/posts'; contents: { #foo->1. #bar->2 } asDictionary; post.
As you can see, the full ZnClient API can be combined when needed.
ZnClient new forJsonREST; post: 'https://jsonplaceholder.typicode.com/posts' contents: (NeoJSONObject new foo: 1; bar: 2; yourself).
#post:contents: combines separate #url: #contents: and #post message.
#forJsonREST uses NeoJSON[Object|Writer] if found, else STONJSON. If both are missing, this results in an error.
ZnClient new systemPolicy; forJsonREST; url: 'http://easy.t3-platform.net/rest/geo-ip'; queryAt: #address put: '81.83.7.35'; get.
Finally, here is a more sophisticated example, doing a DNS request over HTTPS:
ZnClient new systemPolicy; beOneShot; forJsonREST; accept: 'application/dns-json'; url: 'https://cloudflare-dns.com/dns-query'; queryAt: #name put: 'stfx.eu'; queryAt: #type put: #AAAA; get.
Note that in most cases, you will configure one client to a specific endpoint and keep on reusing it. At one point in time it might be good to #close the client (although that happens on finalise as well). For single requests, you can use #beOneShot.
All this can be found in #bleedingEdge (HEAD). There are unit tests as well.
Sven