1

I have curl post to implement in pharo, but it seems like there is not much in term of explanation on how to do that. I saw few example but they ARE way simpler than what I need to. I would you do that inn pharos?

$ curl 'https://url_server' \
-X POST \
-H 'key: MY PASSWORD' \
-H 'Content-Type: application/json' \
-d \
'{
  "HEADER": "FOO",
  "DESK": "POO",
  "FORWARDTO": "another_url"
}'

I know that this is similar to post using Znclient like so:

 ZnClient new
    url: 'url_server';
    entity: (ZnEntity 
            with:'{"HEADER": "FOO", 
                   "DESK": "POO",
                   "FORWARDTO": "another_url"}'
            type: ZnMimeType applicationJson
            );
        post.

However where does the key goes to using this syntax?

eMBee
  • 793
  • 6
  • 16
ludo
  • 543
  • 3
  • 14

1 Answers1

3

Seems like you are looking for the way to set a HTTP header field for your request in Zinc?

Try ZnClient:

headerAt: key put: value
    "Set key equals value in the HTTP header of the current request"

Your code then could look like:

ZnClient new
    url: 'yourURL';
    headerAt: 'headerKey' put: 'headerValue'; 
    entity: (ZnEntity 
        with:'{"yourJSON": "Content"}'
        type: ZnMimeType applicationJson);
    post.

Zinc also has a nice feature, that shows you a curl command line invocation equivalent to the current request. So you can compare to the curl line you had in mind. Just print:

ZnClient new
    url: 'yourURL';
    headerAt: 'headerKey' put: 'headerValue'; 
    entity: (ZnEntity 
        with:'{"yourJSON": "Content"}'
        type: ZnMimeType applicationJson);
    method: #POST;
    curl.

You'll find a good documentation for using Zinc HTTP as client in the Enterprise Pharo book.

MartinW
  • 4,966
  • 2
  • 24
  • 60
  • Thanks @MartinW. But I am using ZnClient here. Is my syntax wrong? should I add the key value as I did for the url? – ludo Mar 09 '18 at 22:23
  • *Zinc* is the name of the framework `ZnClient` belongs to. Hence the *Zn* Prefix for *Zinc* :) – MartinW Mar 10 '18 at 06:55
  • I extended the answer using your example. – MartinW Mar 10 '18 at 07:12
  • Thanks @MartinW This seems more like what I needed. So the key should be in the headerAt – ludo Mar 13 '18 at 07:37
  • Dear @MartinW , Just one last question. How can I check the Json response given by the server after the post done,? – ludo Mar 13 '18 at 20:03
  • @ludo Just check the answer. From the comment of `ZnClinent>>post`: _Execute a HTTP POST on the request set up **and return the response #contents**_ – MartinW Mar 21 '18 at 12:34