[Pharo-dev] Zinc / REST misunderstanding

Sven Van Caekenberghe sven at stfx.eu
Thu Feb 2 12:29:07 EST 2017


Well, now that it works, we can make it better ;-)

This is a more correct way to do your call:

ZnClient new
  beOneShot;
  contentWriter: [ :data | ZnEntity 
    with: (STONJSON toString: data ) 
    type: ZnMimeType applicationJson ];
  url: 'http://server.com/redmine/issues/430.json';
  username: 'anquetil' password: '<mypasswd>';
  contents: { #issue -> { 
    #subject -> 'Subject different changed'. 
    #notes -> 'The subject was changed changed' } asDictionary } asDictionary;
  put.

It is all in one expression now, but there normally is a common configuration part for the ZnClient, and then a part that is different for each call. #contentWriter: is part of the configuration, while #contents: is your data. The content writer takes your data in as objects and turns it into a typed entity. (There is also a corresponding #contentReader: that you can use to parse JSON coming back, if necessary).

Sven

> On 2 Feb 2017, at 17:31, Nicolas Anquetil <nicolas.anquetil at inria.fr> wrote:
> 
> 
> Hi
> 
> thanks Sven, it now works.
> 
> from your answer, I could infer the final solution. Apart from your suggestions, the content-type: also had to be called on ZnClient and voila!
> 
> nicolas
> 
> 
> On 02/02/2017 17:11, Sven Van Caekenberghe wrote:
>>> On 2 Feb 2017, at 16:51, Nicolas Anquetil <nicolas.anquetil at inria.fr> wrote:
>>> 
>>> 
>>> Hi,
>>> 
>>> I am trying to create a small Redmine wrapper with Zinc, using Redmine REST API
>>> 
>>> And I am having a problem to update issues in redmine from zinc.
>>> In curl, I can do:
>>> 
>>> curl -v -H "Content-Type: application/json" -X PUT --data-binary '{
>>>   "issue": {
>>>     "subject": "Subject different changed",
>>>     "notes": "The subject was changed changed"
>>>   }
>>> }'  -u anquetil:<mypasswd> http://<theserver>/redmine/issues/430.json
>>> and this updates issue #430 by changing its subject and notes.
>>> 
>>> For information, the trace given by curl (-v) is:
>>> 
>>> *   Trying <theserver>...
>>>   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
>>>                                  Dload  Upload   Total   Spent    Left  Speed
>>>   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to <theserver> (<theserver>) port 80 (#0)
>>> * Server auth using Basic with user 'anquetil'
>>>> PUT /redmine/issues/430.json HTTP/1.1
>>>> Host: <theserver>
>>>> Authorization: Basic YW5xdWV0aWw6UjNkTTFuMy1wYXNz
>>>> User-Agent: curl/7.47.0
>>>> Accept: */*
>>>> Content-Type: application/json
>>>> Content-Length: 110
>>>> 
>>> } [110 bytes data]
>>> * upload completely sent off: 110 out of 110 bytes
>>> 100   110    0     0  100   110      0    546 --:--:-- --:--:-- --:--:--   544< HTTP/1.1 200 OK
>>> < Date: Thu, 02 Feb 2017 12:18:41 GMT
>>> < Server: Apache/2.2.22 (Ubuntu)
>>> < X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.21
>>> < X-Rack-Cache: invalidate, pass
>>> < X-Request-Id: 73f75565...
>>> < X-UA-Compatible: IE=Edge,chrome=1
>>> < Cache-Control: no-cache
>>> < X-Runtime: 0.994081
>>> < Set-Cookie: _redmine_session=BAh7ByIP...; path=/; HttpOnly
>>> < Set-Cookie: autologin=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
>>> < Status: 200
>>> < Content-Length: 0
>>> < Content-Type: application/json; charset=utf-8
>>> <
>>> 100   110    0     0  100   110      0    105  0:00:01  0:00:01 --:--:--   105
>>> * Connection #0 to host <theserver> left intact
>>> The problem is that I cannot replicate that with Zinc:
>>> 
>>> cont := '{
>>>   "issue": {
>>>     "subject": "Subject different changed",
>>>     "notes": "The subject was changed changed"
>>>   }
>>> }'.
>>> ent := ((ZnHeaders requestHeadersFor: 'http://<theserver>/redmine/' asZnUrl )
>>>     contentType: (ZnMimeType main: 'application' sub: 'json')) ;
>>>     contentLength: cont size.
>>>     ZnClient new
>>>     beOneShot ;
>>>     entity: ent ;
>>>     username: 'anquetil' password: '<mypasswd>' ;
>>>     put: 'http://<theserver>/redmine/issues/430.json' contents: cont ;
>>>     response.
>> You are doing something double (ent & cont are the same). Also, I usually start by setting the URL and continue from there. Could you try:
>> 
>> ZnClient new
>>     beOneShot ;
>>     url: 'http://<theserver>/redmine/issues/430.json' ;
>>     entity: ent ;
>>     username: 'anquetil' password: '<mypasswd>' ;
>>     put ;
>>     yourself.
>> 
>> I would inspect the instance so you can see both the request and response.
>> 
>> Does this work ?
>> 
>> Also, the JSON can be generated from objects instead of being written as a string, but you knew that, right ?
>> 
>>> if I inspect the ZnHeader constructed, it seems to have all headers that curl traces except the Authorization: Basic one:
>>> 
>>> <cicgeiiljjcidaam.png>
>>> 
>>> The response of ZnClient is "200 OK", with again the same headers as reported by curl
>>> 
>>> <mjlnpnjhhegjmhnl.png>
>>> 
>>> Yet the issue remains unchanged on the redmine server (contrary to curl evidently)
>>> 
>>> Can anyone explain where the error could be ?
>>> Any help will be gladly welcome
>>> 
>>> nicolas
>>> 
>>> -- 
>>> Nicolas Anquetil -- MCF (HDR)
>>> Project-Team RMod
>>> 
>> 
> 
> -- 
> Nicolas Anquetil -- MCF (HDR)
> Project-Team RMod
> 
> 




More information about the Pharo-dev mailing list