Rest post methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Rest post methods

Pharo Smalltalk Users mailing list
Hello Everybody.
I am implementing an architecture of rest services like in TinyBlog
tutorial. Now I want to implement post
methods. But it doesn't work. My
execute function
execute
    | user u |
   
    [
        user := NeoJSONReader fromString: self context request rawBody.
        u := User new pseudo: (user at: #pseudo);
                numero_telephone: (user at: #numero_telephone).
                user save.
     ] on: Error do: [ self context request badRequest ].
        self dataType: WAMimeType textPlain with: ''

neojsonmapping on class User

neoJsonMapping: mapper
    mapper for: self do: [ :mapping | mapping mapAllInstVars ]

The restfilter consumes */json.

I test it with ZnClient but that is not working.
ZnClient new
        url: 'http://localhost:8080/Application/users/add';
        formAt: 'pseudo' put: 'Big man';
        formAt: 'numero_telephone' put: '99546321';
        post.
 
I don't know what is the matter.

Thanks.

Asbath


Reply | Threaded
Open this post in threaded view
|

Re: Rest post methods

Sven Van Caekenberghe-2
Hi Asbath,

From what I can see in the code included in the email, you wrote a POST handler that expects/accepts JSON, while the client call sends a application/x-www-form-urlencoded payload.

You need to send JSON data. The following creates a client that sends and expects JSON.

ZnClient new
  url: 'http://localhost:8888/test'l;
  accept: ZnMimeType applicationJson;
  contentReader: [ :entity | entity ifNotNil: [ NeoJSONReader fromString: entity contents ] ];
  contentWriter: [ :object | ZnEntity with: (NeoJSONWriter toString: object) type: ZnMimeType applicationJson ].

To send data along, you can just pass it using #contents:

client contents: { #username->'john'. #password->'secret } asDictionary.

The #contentWriter will do the conversion. Finally, you execute using #get, #put, #post or #delete. You could also use any object that has a #neoJsonMapping.

Have a look at section 11.3 in https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/Zinc-HTTP-Server/Zinc-HTTP-Server.html as well as the whole chapter.

HTH,

Sven

> On 2 Jan 2017, at 18:41, Asbath Sama biyalou via Pharo-users <[hidden email]> wrote:
>
>
> From: Asbath Sama biyalou <[hidden email]>
> Subject: Rest post methods
> Date: 2 January 2017 at 18:41:30 GMT+1
> To: Pharo users users <[hidden email]>
>
>
> Hello Everybody.
> I am implementing an architecture of rest services like in TinyBlog
> tutorial. Now I want to implement post
> methods. But it doesn't work. My
> execute function
> execute
>    | user u |
>
>    [
>        user := NeoJSONReader fromString: self context request rawBody.
>        u := User new pseudo: (user at: #pseudo);
>                numero_telephone: (user at: #numero_telephone).
>                user save.
>     ] on: Error do: [ self context request badRequest ].
>        self dataType: WAMimeType textPlain with: ''
>
> neojsonmapping on class User
>
> neoJsonMapping: mapper
>    mapper for: self do: [ :mapping | mapping mapAllInstVars ]
>
> The restfilter consumes */json.
>
> I test it with ZnClient but that is not working.
> ZnClient new
>        url: 'http://localhost:8080/Application/users/add';
>        formAt: 'pseudo' put: 'Big man';
>        formAt: 'numero_telephone' put: '99546321';
>        post.
>
> I don't know what is the matter.
>
> Thanks.
>
> Asbath
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Rest post methods

Pharo Smalltalk Users mailing list
Thanks Sven.

That is fine. According to your answer this is not a problem of
implementation of rest methods.

But It is a problem of client. Then Have you an idea on how to do that
client on Android?

Cheers

Asbath


On 02/01/2017 18:57, Sven Van Caekenberghe wrote:

> Hi Asbath,
>
> From what I can see in the code included in the email, you wrote a POST handler that expects/accepts JSON, while the client call sends a application/x-www-form-urlencoded payload.
>
> You need to send JSON data. The following creates a client that sends and expects JSON.
>
> ZnClient new
>   url: 'http://localhost:8888/test'l;
>   accept: ZnMimeType applicationJson;
>   contentReader: [ :entity | entity ifNotNil: [ NeoJSONReader fromString: entity contents ] ];
>   contentWriter: [ :object | ZnEntity with: (NeoJSONWriter toString: object) type: ZnMimeType applicationJson ].
>
> To send data along, you can just pass it using #contents:
>
> client contents: { #username->'john'. #password->'secret } asDictionary.
>
> The #contentWriter will do the conversion. Finally, you execute using #get, #put, #post or #delete. You could also use any object that has a #neoJsonMapping.
>
> Have a look at section 11.3 in https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/Zinc-HTTP-Server/Zinc-HTTP-Server.html as well as the whole chapter.
>
> HTH,
>
> Sven
>
>> On 2 Jan 2017, at 18:41, Asbath Sama biyalou via Pharo-users <[hidden email]> wrote:
>>
>>
>> From: Asbath Sama biyalou <[hidden email]>
>> Subject: Rest post methods
>> Date: 2 January 2017 at 18:41:30 GMT+1
>> To: Pharo users users <[hidden email]>
>>
>>
>> Hello Everybody.
>> I am implementing an architecture of rest services like in TinyBlog
>> tutorial. Now I want to implement post
>> methods. But it doesn't work. My
>> execute function
>> execute
>>    | user u |
>>
>>    [
>>        user := NeoJSONReader fromString: self context request rawBody.
>>        u := User new pseudo: (user at: #pseudo);
>>                numero_telephone: (user at: #numero_telephone).
>>                user save.
>>     ] on: Error do: [ self context request badRequest ].
>>        self dataType: WAMimeType textPlain with: ''
>>
>> neojsonmapping on class User
>>
>> neoJsonMapping: mapper
>>    mapper for: self do: [ :mapping | mapping mapAllInstVars ]
>>
>> The restfilter consumes */json.
>>
>> I test it with ZnClient but that is not working.
>> ZnClient new
>>        url: 'http://localhost:8080/Application/users/add';
>>        formAt: 'pseudo' put: 'Big man';
>>        formAt: 'numero_telephone' put: '99546321';
>>        post.
>>
>> I don't know what is the matter.
>>
>> Thanks.
>>
>> Asbath
>>
>>
>>
>>


Reply | Threaded
Open this post in threaded view
|

Re: Rest post methods

Sven Van Caekenberghe-2
In reply to this post by Sven Van Caekenberghe-2

> On 2 Jan 2017, at 20:42, Asbath Sama biyalou <[hidden email]> wrote:
>
> Thanks Sven.
>
> That is fine. According to your answer this is not a problem of
> implementation of rest methods.

Indeed.

> But It is a problem of client.

Yes.

> Then Have you an idea on how to do that client on Android?

I am afraid not.

The chapter of the Pharo Enterprise book that I gave contains examples of how to do it using curl, which is a *nix command line http client. From there you should be able to find your way. Good luck, it should not be too hard.

> Cheers
>
> Asbath
>
>
> On 02/01/2017 18:57, Sven Van Caekenberghe wrote:
>> Hi Asbath,
>>
>> From what I can see in the code included in the email, you wrote a POST handler that expects/accepts JSON, while the client call sends a application/x-www-form-urlencoded payload.
>>
>> You need to send JSON data. The following creates a client that sends and expects JSON.
>>
>> ZnClient new
>>  url: 'http://localhost:8888/test'l;
>>  accept: ZnMimeType applicationJson;
>>  contentReader: [ :entity | entity ifNotNil: [ NeoJSONReader fromString: entity contents ] ];
>>  contentWriter: [ :object | ZnEntity with: (NeoJSONWriter toString: object) type: ZnMimeType applicationJson ].
>>
>> To send data along, you can just pass it using #contents:
>>
>> client contents: { #username->'john'. #password->'secret } asDictionary.
>>
>> The #contentWriter will do the conversion. Finally, you execute using #get, #put, #post or #delete. You could also use any object that has a #neoJsonMapping.
>>
>> Have a look at section 11.3 in https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/Zinc-HTTP-Server/Zinc-HTTP-Server.html as well as the whole chapter.
>>
>> HTH,
>>
>> Sven
>>
>>> On 2 Jan 2017, at 18:41, Asbath Sama biyalou via Pharo-users <[hidden email]> wrote:
>>>
>>>
>>> From: Asbath Sama biyalou <[hidden email]>
>>> Subject: Rest post methods
>>> Date: 2 January 2017 at 18:41:30 GMT+1
>>> To: Pharo users users <[hidden email]>
>>>
>>>
>>> Hello Everybody.
>>> I am implementing an architecture of rest services like in TinyBlog
>>> tutorial. Now I want to implement post
>>> methods. But it doesn't work. My
>>> execute function
>>> execute
>>>   | user u |
>>>
>>>   [
>>>       user := NeoJSONReader fromString: self context request rawBody.
>>>       u := User new pseudo: (user at: #pseudo);
>>>               numero_telephone: (user at: #numero_telephone).
>>>               user save.
>>>    ] on: Error do: [ self context request badRequest ].
>>>       self dataType: WAMimeType textPlain with: ''
>>>
>>> neojsonmapping on class User
>>>
>>> neoJsonMapping: mapper
>>>   mapper for: self do: [ :mapping | mapping mapAllInstVars ]
>>>
>>> The restfilter consumes */json.
>>>
>>> I test it with ZnClient but that is not working.
>>> ZnClient new
>>>       url: 'http://localhost:8080/Application/users/add';
>>>       formAt: 'pseudo' put: 'Big man';
>>>       formAt: 'numero_telephone' put: '99546321';
>>>       post.
>>>
>>> I don't know what is the matter.
>>>
>>> Thanks.
>>>
>>> Asbath
>>>
>>>
>>>
>>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Rest post methods

Pharo Smalltalk Users mailing list

Thanks Sven.
I will try it.

On 02/01/2017 23:49, Sven Van Caekenberghe wrote:

>> On 2 Jan 2017, at 20:42, Asbath Sama biyalou <[hidden email]> wrote:
>>
>> Thanks Sven.
>>
>> That is fine. According to your answer this is not a problem of
>> implementation of rest methods.
> Indeed.
>
>> But It is a problem of client.
> Yes.
>
>> Then Have you an idea on how to do that client on Android?
> I am afraid not.
>
> The chapter of the Pharo Enterprise book that I gave contains examples of how to do it using curl, which is a *nix command line http client. From there you should be able to find your way. Good luck, it should not be too hard.
>
>> Cheers
>>
>> Asbath
>>
>>
>> On 02/01/2017 18:57, Sven Van Caekenberghe wrote:
>>> Hi Asbath,
>>>
>>> From what I can see in the code included in the email, you wrote a POST handler that expects/accepts JSON, while the client call sends a application/x-www-form-urlencoded payload.
>>>
>>> You need to send JSON data. The following creates a client that sends and expects JSON.
>>>
>>> ZnClient new
>>>  url: 'http://localhost:8888/test'l;
>>>  accept: ZnMimeType applicationJson;
>>>  contentReader: [ :entity | entity ifNotNil: [ NeoJSONReader fromString: entity contents ] ];
>>>  contentWriter: [ :object | ZnEntity with: (NeoJSONWriter toString: object) type: ZnMimeType applicationJson ].
>>>
>>> To send data along, you can just pass it using #contents:
>>>
>>> client contents: { #username->'john'. #password->'secret } asDictionary.
>>>
>>> The #contentWriter will do the conversion. Finally, you execute using #get, #put, #post or #delete. You could also use any object that has a #neoJsonMapping.
>>>
>>> Have a look at section 11.3 in https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/Zinc-HTTP-Server/Zinc-HTTP-Server.html as well as the whole chapter.
>>>
>>> HTH,
>>>
>>> Sven
>>>
>>>> On 2 Jan 2017, at 18:41, Asbath Sama biyalou via Pharo-users <[hidden email]> wrote:
>>>>
>>>>
>>>> From: Asbath Sama biyalou <[hidden email]>
>>>> Subject: Rest post methods
>>>> Date: 2 January 2017 at 18:41:30 GMT+1
>>>> To: Pharo users users <[hidden email]>
>>>>
>>>>
>>>> Hello Everybody.
>>>> I am implementing an architecture of rest services like in TinyBlog
>>>> tutorial. Now I want to implement post
>>>> methods. But it doesn't work. My
>>>> execute function
>>>> execute
>>>>   | user u |
>>>>
>>>>   [
>>>>       user := NeoJSONReader fromString: self context request rawBody.
>>>>       u := User new pseudo: (user at: #pseudo);
>>>>               numero_telephone: (user at: #numero_telephone).
>>>>               user save.
>>>>    ] on: Error do: [ self context request badRequest ].
>>>>       self dataType: WAMimeType textPlain with: ''
>>>>
>>>> neojsonmapping on class User
>>>>
>>>> neoJsonMapping: mapper
>>>>   mapper for: self do: [ :mapping | mapping mapAllInstVars ]
>>>>
>>>> The restfilter consumes */json.
>>>>
>>>> I test it with ZnClient but that is not working.
>>>> ZnClient new
>>>>       url: 'http://localhost:8080/Application/users/add';
>>>>       formAt: 'pseudo' put: 'Big man';
>>>>       formAt: 'numero_telephone' put: '99546321';
>>>>       post.
>>>>
>>>> I don't know what is the matter.
>>>>
>>>> Thanks.
>>>>
>>>> Asbath
>>>>
>>>>
>>>>
>>>>