benibela / xidel

Command line tool to download and extract data from HTML/XML pages or JSON-APIs, using CSS, XPath 3.0, XQuery 3.0, JSONiq or pattern matching. It can also create new or transformed XML/HTML/JSON documents.
http://www.videlibri.de/xidel.html
GNU General Public License v3.0
681 stars 42 forks source link

POSTing JSON data impossible/needs double curly brackets #44

Closed tgoeg closed 4 years ago

tgoeg commented 4 years ago

I am trying to send JSON data to an API.

user@host:~$ xidel -H "Content-Type: application/json" -d '{"method":"get_session_key"}' $url -e "result" --verbose
Error:
err:XPST0003: "}" expected, but ":" found
in: {"method": [<- error occurs before here] "get_session_key"}

I was unable to use the json-serialize() function to do the actualy JSON structuring, but I am a complete newbie to xpath/xquery and xidel. Maybe that's not possible at all.

Sending JSON however is possible by using double curly brackets.

I don't know if this is intended - at least it is not documented. But this works:

user@host:~$ xidel -H "Content-Type: application/json" -d '{{"method":"get_session_key"}}' $url -e "result" --verbose

What is the intended way of sending/posting JSON data and parsing the result again (in bash)?

benibela commented 4 years ago

Double curly brackets is the intended way.

It's a "feature". Single curly brackets are for calculations. E.g. -d "{1 + 2 + 3}" would send 6.

The function can be used like: -d '{serialize-json({"method":"get_session_key"})}'

Parsing happens automatically. It creates a variable $json for use in the -e expression. The xidel 0.9.9 development builds also support the xpath 3.1 syntax, ?key or ?* to read values

tgoeg commented 4 years ago

Thanks, I understand now. This should probably be part of the documentation!