Am 02.04.2014 14:59, schrieb Dmitry Dneprov:
> 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
>
Hi,
it can be configured with mapping. Note that the mapping use regular
expressions to match the url. Also the http method can be used to
configure mapping. For example in tntnet.xml you can use:
<mapping>
<target>delete_products@myapp</target>
<url>^/products/(.*)</url>
<method>^DELETE$</method>
<args>
<id>$1</id>
</args>
</mapping>
This calls the component "delete_products" on a DELETE request. The
product id is passed as arguments and can be accessed in the component
using:
<%cpp>
std::string id = request.getArg("id");
</%cpp>
Or You can go even further:
<mapping>
<target>delete_$1@myapp</target>
<url>^/([^/]*)/(.*)</url>
<method>^DELETE$</method>
<args>
<id>$2</id>
</args>
</mapping>
Note that the regex ([^/]) matches the first part of the url before the
first /. So you map e.g. "/foobar/4711" to "delete_foobar" or
"/baz/6677" to "baz".
Now you just need mappings for each method you wish to support and
tntnet calls the component, which does the right thing.
Tommi
------------------------------------------------------------------------------
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general