本文介绍了使用 BaseX XQuery 代码处理仅 JSON 的 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近接手了一个旧的代码项目来完成一项新工作,这个项目广泛使用了存储在 BaseX 中的 XML 数据库.核心项目是一个 Node Web 应用程序,它将各种数据库操作委托给与 BaseX 数据库交互的 XQuery 函数.

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.

最近,我们用来交叉发布公共数据的外部 API(一种公共数据中心)彻底改变了它的 API,并且只接受 JSON 编码的 REST API 调用.这对我们来说是个问题,因为我们使用 BaseX/XQuery 方法来塑造 XML 编码的 REST 调用,而且效果非常好.

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.

我彻底查看了 BaseX 各种模块(特别是 JSON 和 HTTP 模块)的文档,但似乎 XML 元素是 http:send-request 函数接受的唯一内容我们正在使用 XQuery 进行调用.BaseX 显然能够解析和序列化 JSON 数据,但我找不到任何可以发送 JSON 数据的 HTTP 请求.

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.

我本来想在 Node 应用程序代码中执行这个外部发布而不是 XQuery 方法,但是发布的内容依赖于从数据库中获取的各种数据,所以感觉有点适得其反.

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.

我在这里遗漏了什么吗?是否可以使用来自 BaseX 数据库的 XML 数据在 XQuery 中塑造和发送 JSON 数据?

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

提前致谢.

推荐答案

习惯 EXPath HTTP 客户端模块.这是使用 POST 方法将 JSON 数据发送到 Web 服务的一种方法:

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" }'
)

实际的 JSON 负载是通过第三个函数参数提供的.在示例中,它是一个普通字符串.如果您的输入是 XML,您可以通过 json:serialize 将其转换为字符串表示形式.

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.

这篇关于使用 BaseX XQuery 代码处理仅 JSON 的 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:38