eXist-db 使用 eXPath Http_module 通过 POST 请求发送 JSON [英] eXist-db send JSON via POST request using eXPath Http_module
问题描述
我正在尝试通过 eXist-db 的 POST 请求将 JSON 发送到使用 eXPath HTTP 模块的 API,但我总是收到以下错误:
exerr:ERROR 序列化正文内容时出错
我的 XQuery 看起来像:
xquery 版本3.1";声明命名空间输出=http://www.w3.org/2010/xslt-xquery-serialization";让 $payload := 地图 {名称":测试用户",角色":用户"}返回hc:发送请求(<hc:request method='post'><hc:body media-type="application/json"/></hc:request>,'https://reqres.in/api/users',序列化($payload,<输出:序列化参数><output:method>json</output:method></output:serialization-parameters>))[2] =>util:base64-decode() =>解析-json()
预期输出(通过 Postman 测试):
<代码>{名称":测试用户",id":820",createdAt":2021-06-03T05:50:32.049Z"}
如何将内容序列化为 JSON 并通过来自 eXist-db 的 POST 请求发送?解决方案提示 here 不适用于我.
解决方案是在元素中添加
@method="text"
.
EXPath HTTP 客户端规范中记录了此属性,部分 3.2 序列化请求内容一个>:
<块引用>序列化方法的默认值取决于媒体类型:如果是XML媒体类型则为xml
,如果是HTML媒体类型则为html
, xhtml
如果是 application/xhtml+xml, text
如果是文本媒体类型,binary
是其他情况.>
这意味着 EXPath HTTP 客户端没有针对 application/json
媒体类型的内置映射——即,它依赖于 binary代码>.但是 JSON 是 一种文本媒体类型,并且您已经通过
fn:serialize()
函数将 XDM 映射序列化为文本.因此,您必须覆盖 method="binary"
的 EXPath HTTP 客户端默认值,并在 上设置
method="text"
代码>元素.
草案 v2 规范,简化发送JSON;默认情况下,当它检测到正文包含 map()
或 array()
时,它将使用适当的媒体类型将项目序列化为 JSON.>
但是现在对于 v1,我们仍然需要显式 (1) 使用 JSON 序列化方法序列化映射和数组,以及 (2) 在 < 上指定
元素.method="text"
;body>
I am trying to send JSON via a POST request from eXist-db to an API using the eXPath HTTP-Module, but I'm always recieving the following Error:
exerr:ERROR Error serializing the body content
My XQuery looks like:
xquery version "3.1";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
let $payload := map {
"name": "testuser",
"role": "user"
}
return
hc:send-request(
<hc:request method='post'>
<hc:body media-type="application/json"/>
</hc:request>,
'https://reqres.in/api/users',
serialize($payload, <output:serialization-parameters>
<output:method>json</output:method>
</output:serialization-parameters>)
)[2] => util:base64-decode() => parse-json()
Expected Output (tested via Postman):
{
"name": "testuser",
"id": "820",
"createdAt": "2021-06-03T05:50:32.049Z"
}
How do i serialize content to JSON and send it via a POST-request from eXist-db? The solution prompted here is not working for me.
The solution is to add @method="text"
to the <body>
element.
This attribute is documented in the EXPath HTTP Client spec, section 3.2 Serializing the request content:
The default value of the serialization method depends on the media-type: it is
xml
if it is an XML media type,html
if it is an HTML media type,xhtml
if it is application/xhtml+xml,text
if it is a textual media type, andbinary
for any other case.
What this means is that the EXPath HTTP Client doesn't have a built-in mapping for the media-type of application/json
—i.e., it falls back on binary
. But JSON is a textual media type, and you've already performed the serialization of your XDM map into text via the fn:serialize()
function. So you have to override the EXPath HTTP Client default of method="binary"
and set method="text"
on the <body>
element.
The draft v2 spec, simplifies sending JSON; by default, when it detects that the body contains a map()
or array()
, it will serialize the item as JSON, with the appropriate media-type.
But for now with v1, we still need to explicitly (1) serialize maps and arrays using the JSON serialization method and (2) specify method="text"
on the <body>
element.
这篇关于eXist-db 使用 eXPath Http_module 通过 POST 请求发送 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!