httpxquerybasex

How to send a xquery to a BaseX database via http


I am trying to use the http interface to access a BaseX database.

The setup is as follows:

`   <?xml version="1.0" encoding="UTF-8"?>
    <musicCollection>
        <entry>
            <Type>CD</Type>
            <Name>Greatest Hits</Name>
            <Artist>The Music Band</Artist>
            <Year>1998</Year>
            <Songs>
                <Song>
                    <Name>Song 1</Name>
                    <Author>John Songwriter</Author>
                    <Length>4:30</Length>
                </Song>
                <Song>
                    <Name>Song 2</Name>
                    <Author>Lisa Lyricist</Author>
                    <Length>3:45</Length>
                </Song>
                <Song>
                    <Name>Song 3</Name>
                    <Author>Lisa Lyricist</Author>
                    <Length>1:45</Length>
                </Song>
            </Songs>
        </entry>
        .... more entries
    </musicCollection>
`

java -cp BaseX.jar org.basex.BaseXClient ... login in as test / test ...

at the prompt:

xquery for $e in doc('music')/musicCollection/entry where $e/Type = "LP" return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>

Result:

<entry><Type>LP</Type><Name>Rock Revolution</Name><Artist>The Rockers</Artist></entry>
<entry><Type>LP</Type><Name>Rock Revolution</Name><Artist>The Other Ones</Artist></entry>
Query "BaseX" executed in 6.44 ms.  
"D:\Programs\BaseX\bin\basexhttp.bat" -h123
curl -u test:test -X POST -d "for $e in doc('music')/musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>" "http://localhost:123/rest"

and this:

curl -u test:test -X POST -d "for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>" "http://localhost:123/rest/music"

` I got this error reply:

"" (Line 1): Content ist nicht zulässig in Prolog. (Line 1: content is illegal in prolog)

trying this:

curl -u test:test -X POST -d "<query>for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry></query>" "http://localhost:123/rest/music"

i got:

Stopped at D:/Programs/BaseX/webapp, 1/1:
[XPST0003] Empty query.

with this:

curl -u test:test -X POST -d "<query><text>for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry></text></query>" "http://localhost:123/rest/music"

i got this error:

Stopped at D:/Programs/BaseX/webapp, 1/41:
[XPST0003] Expecting return value.

What is the correct way to send this query?


Solution

  • Your last attempt to send the query with the POST method was pretty close: If your XQuery string includes angle brackets or XML entities, you’ll need to use CDATA to prevent your query characters from being interpreted as XML:

    curl
      -u test:test
      -X POST
      -d "<query><text><![CDATA[for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>]]></text></query>"
      "http://localhost:123/rest/music"
    

    The CDATA section is not required if your query string is unambiguous:

    curl
      -u test:test
      -X POST
      -d "<query><text>/musicCollection/entry/(element entry {Type,Name,Artist})</text></query>"
      "http://localhost:123/rest/music"