我们如何在pharo中实现卷曲帖子

时间:2018-03-09 13:24:07

标签: curl smalltalk pharo pharo-5

我已经在pharo中实施curl post,但似乎没有太多关于如何做到这一点的解释。 我看到了一些例子,但它们比我需要的更简单。 我会那样做客栈pharos吗?

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

我知道这与使用Znclient的帖子类似:

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

然而,使用这种语法的关键在哪里?

1 个答案:

答案 0 :(得分: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.