[xwiki-users] get JSON with REST API
Hi everybody, I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API. This is my script : {{velocity}} #if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end #set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end #set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}") #end #set($fin="]") #set($json= "${json} ${fin}") $json {{/velocity}} I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard. Someone could help me ? Thanks in advance for your answers. Pierrick
You are mixing too things, REST API access data. There is no support for providing "official" REST reources from pages yet. If you want to get the result of your script you need to access it through http://myhost/xwiki/get/MySpace/MyDocument?ouputSyntax=plain On Fri, May 24, 2013 at 9:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
Ok, it solves my problem. Thanks a lot Thomas ! I have an other question : is it possible to specify the wiki when we do a hql or xwql query ? I saw there is a "setWiki()" method which works only if the user have programming rights. Is it possible to do this without these rights ? On Fri, 24 May 2013 10:01:49 +0200, Thomas Mortagne <[email protected]> wrote:
You are mixing too things, REST API access data. There is no support for providing "official" REST reources from pages yet.
If you want to get the result of your script you need to access it through http://myhost/xwiki/get/MySpace/MyDocument?ouputSyntax=plain
On Fri, May 24, 2013 at 9:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Fri, May 24, 2013 at 10:37 AM, <[email protected]> wrote:
Ok, it solves my problem.
Thanks a lot Thomas !
I have an other question : is it possible to specify the wiki when we do a hql or xwql query ? I saw there is a "setWiki()" method which works only if the user have programming rights. Is it possible to do this without these rights ?
The fact that it require PR was actually a bug fixed in https://jira.xwiki.org/browse/XWIKI-7756 (4.1). I updated http://extensions.xwiki.org/xwiki/bin/view/Extension/Query+Module documentation.
On Fri, 24 May 2013 10:01:49 +0200, Thomas Mortagne <[email protected]> wrote:
You are mixing too things, REST API access data. There is no support for providing "official" REST reources from pages yet.
If you want to get the result of your script you need to access it through http://myhost/xwiki/get/MySpace/MyDocument?ouputSyntax=plain
On Fri, May 24, 2013 at 9:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
On Fri, May 24, 2013 at 10:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
It is advisable to build the JSON in memory using objects like maps, arrays, etc. and then serialize the 'root' object using the JSON Velocity tool http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HJSONTo... . One of the advantages with this approach is that you don't have to care about escaping JSON values (in your code above any of the strings you put in the JSON can contain special characters that would break the JSON). And you don't need REST to access the generated JSON. Just use the page URL (/xwiki/bin/get/Space/Page?outputSyntax=plain). Hope this helps, Marius
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Fri, May 24, 2013 at 10:54 AM, Marius Dumitru Florea <[email protected]> wrote:
On Fri, May 24, 2013 at 10:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
It is advisable to build the JSON in memory using objects like maps, arrays, etc. and then serialize the 'root' object using the JSON Velocity tool http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HJSONTo... . One of the advantages with this approach is that you don't have to care about escaping JSON values (in your code above any of the strings you put in the JSON can contain special characters that would break the JSON).
I would add that you should use {{velocity wiki="false"}} as otherwise your JSON is going to be parsed as wiki syntax.
And you don't need REST to access the generated JSON. Just use the page URL (/xwiki/bin/get/Space/Page?outputSyntax=plain).
Hope this helps, Marius
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
participants (3)
-
Marius Dumitru Florea -
pierrick.bena@intech.lu -
Thomas Mortagne