1

I recently took over an older code project for a new job, and this project extensively uses XML databases stored in BaseX. The core project is a Node web application delegating various database operations to XQuery functions interacting with the BaseX databases.

Recently, an external API that we were using to cross-publish our public data (a sort of public data hub) completely changed its API, and only accepts JSON encoding for its REST API calls. This is a problem for us, because we were using our BaseX/XQuery methods to shape XML-encoded REST calls and it worked really well.

I looked thoroughly at the documentation for various modules of BaseX (JSON & HTTP modules in particular) but it does seem like XML elements are the only things accepted by the http:send-request function we are using to make our calls in XQuery. BaseX is apparently able to parse and serialize JSON data but I could not find anything to send JSON data for HTTP requests.

I was thinking of performing this external publication in the Node app code instead of the XQuery methods, but the contents of the publication are relying on various data fetched from the database, so it does feel a little counter-productive.

Am I missing something here? Is it possible to use XML data from BaseX databases to shape and send JSON data in XQuery?

Thanks in advance.

RdNetwork
  • 41
  • 8

1 Answers1

4

It takes a while to get used to the EXPath HTTP Client Module. Here is one way to send JSON data to a web service with the POST method:

http:send-request(
  <http:request method='post'>
    <http:body media-type="application/json"/>
  </http:request>,
  'https://your/url',
  '{ "a": "b" }'
)

The actual JSON payload is supplied via the third function argument. In the example, it’s a plain string. If your input is XML, you can convert it to a string representation via json:serialize.

Christian Grün
  • 6,012
  • 18
  • 34
  • Interesting. I did not understand that you could simply pass a string as third parameter. Thanks! I assume that'll do the trick. – RdNetwork Feb 16 '21 at 15:01