Hello,

I too have been trying figure out how to process JSON from GET and POST 
requests using tntnet.

I do not know what the best way is, but I have discovered, through trial and 
error, a way. If anyone knows of a better method, please let me know. 

If one has some simple JSON such as 

{"String":"abcd"}

simply adding

<%args>
        std::string String;
</%args>        

to the receiving module will work. The variables can then be processed 
(magically) as if they came from a normal GET or POST rather than something 
involving JSON.

If one has more complex JSON such as

[{"ID":"5","String":"abcd"},{"User":"3","ViewData":true,"EditData":true},{"User”:"5","ViewData":true,"EditData":true}{"User”:"7","ViewData":false,"EditData":true}{"User”:"9","ViewData":false,"EditData":false}]

one has to write a deserializer for the receiving module. A example to follow 
can be found at http://pastebin.com/HLJLbTSE

To feed the data into the deserializer one needs to add something like this to 
the receiving module (I have only tested POST, but I assume it will also work 
for GET):

std::string z;
if(qparam.paramcount()){
 z = tnt::stringTo<std::string>("z", "std::string", qparam.param(0), 
reply.out().getloc());
}
std::istringstream in(z);
cxxtools::JsonDeserializer unjson(in);

Please note that qparam is not declared in the .ecpp file. The declaration is 
added when ecpp converts it to .cpp.

For the record, I am using Zepto rather than jQuery, but I don’t think that 
should matter at all.

Damon


On 2014 Apr 2, at 8:59 AM, Dmitry Dneprov <[email protected]> wrote:

> Hello,
> 
> What is the best way to create REST services with TNTNET?
> TNTNET web site have a great howto on using TNTNET with jQuery,
> but it describes only part of the task (GET).
> Real REST service is usually bi-directional. For instance, I want to have 
> URLs:
> http://mysite.com/products/<id>
> Where I can use POST, DELETE and GET operations.
> So the questions are:
> 1. What is the best way to handle POST and DELETE operations
> to process JSON requests with TNTNET ?
> 2. What is the best way to handle REST URLs: http://mysite.com/products/<id> ?
> without creating separate components for every product <id>.
> 
> Regards, Dmitry
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Tntnet-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/tntnet-general

--------------------------
Damon Little
Assistant Curator of Bioinformatics
Cullman Program for Molecular Systematics
The New York Botanical Garden
Bronx, New York 10458-5126
U.S.A.
Tel: 718.817.8521
------------------------------------------------------------------------------
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to