Properly sending/receiving JSON?
Saturday, 23 December 2017 · 2 messages · last reply 24 Dec 2017
RC
Robert Crews23 Dec 2017 03:57 #
First off, thank you for the fast answer to my previous question.
Now, I'm having trouble understanding how to send JSON to BaseX. Ultimately, I want to send JSON and store it (allowing BaseX to convert it to XML first, of course).
For now, however, I am having trouble just receiving the JSON data. Here's my test XQM file:
module namespace hwxdocs = "urn:com:hortonworks:docs";
declare
%rest:POST("{$body}")
%rest:path("hello/json")
%rest:consumes("application/json")
%output:media-type("application/json")
function hwxdocs:hello2(
$body
) {
"Request body: " || $body
};
I send data with this command:
curl -iL -X POST \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data-binary '{"foo":{"bar":"baz"}}' \
http://localhost:8984/hello/json
I expect to get
Request body: {"foo":{"bar":"baz"}}
(I’m assuming I can pass '{"foo":{"bar":"baz”}}’ to
json:parse to convert to a BaseX XML that can be stored in the database.) However, instead, I get
Request body: baz
Can someone tell me what my error is?
Thank you,
RobertCG
Christian Grün24 Dec 2017 12:19 #
Hi Robert,
I haven’t run your code. I assume that your RESTXQ code will already give you an XML representation of your JSON data, because, by default, the 'direct' format will be applied [1,2].
The expression…
"Request body: " || $body
…will atomize your body and return it as string. Just remove the string concatenation and return $body, then it will be more obvious what’s going on.
Cheers,
Christian
[1] http://docs.basex.org/wiki/RESTXQ#Content_Types
[2] http://docs.basex.org/wiki/JSON_Module#Options
Quoted text · Robert Crews · 34 lines
> module namespace hwxdocs = "urn:com:hortonworks:docs"; > declare > %rest:POST("{$body}") > %rest:path("hello/json") > %rest:consumes("application/json") > %output:media-type("application/json") > function hwxdocs:hello2( > $body > ) { > "Request body: " || $body > }; > > I send data with this command: > > curl -iL -X POST \ > --header 'Content-Type: application/json' \ > --header 'Accept: application/json' \ > --data-binary '{"foo":{"bar":"baz"}}' \ > http://localhost:8984/hello/json > > I expect to get > > Request body: {"foo":{"bar":"baz"}} > > (I’m assuming I can pass '{"foo":{"bar":"baz”}}’ to json:parse to convert to a BaseX XML that can be stored in the database.) > However, instead, I get > > Request body: baz > > Can someone tell me what my error is? > > Thank you, > Robert >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery