我很好奇是否有可能在Nest.js的请求正文中获取XML数据。
"dependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
我希望有一个名为/EPCIS/capture
的HTTP POST API,它将获取如下所示的XML文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<epcis:EPCISDocument
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:epcis="urn:epcglobal:epcis:xsd:1"
xmlns:epcglobal="urn:epcglobal:xsd:1"
xsi:schemaLocation="urn:epcglobal:epcis:xsd:1 EPCglobal-epcis-1_0.xsd"
creationDate="2008-03-16T22:13:16.397+01:00"
schemaVersion="1.0">
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime>2008-03-16T22:13:16.397+01:00</eventTime>
<eventTimeZoneOffset>+01:00</eventTimeZoneOffset>
<epcList>
<epc>urn:epc:id:sgtin:0614141.107346.2017</epc>
<epc>urn:epc:id:sgtin:0614141.107346.2018</epc>
</epcList>
<action>OBSERVE</action>
<bizStep>urn:epcglobal:epcis:bizstep:fmcg:shipped</bizStep>
<disposition>urn:epcglobal:epcis:disp:fmcg:unknown</disposition>
<readPoint>
<id>urn:epc:id:sgln:0614141.07346.1234</id>
</readPoint>
<bizLocation>
<id>urn:epcglobal:fmcg:loc:0614141073467.A23-49</id>
</bizLocation>
<bizTransactionList>
<bizTransaction type="urn:epcglobal:fmcg:btt:po">
http://transaction.acme.com/po/12345678
</bizTransaction>
</bizTransactionList>
</ObjectEvent>
</EventList>
</EPCISBody>
</epcis:EPCISDocument>
在我的控制器内:
Post('capture')
addEPCDocument(@Body() epcDocument: any): any {
console.log(epcDocument)
}
但是记录进入的请求正文时,我只得到{}
。我的POSTMAN设置已经提到:
Content-Type: application/xml
并且在Body
中,我粘贴了上述XML。响应为HTTP 400错误请求。
通常从Nest.JS的请求正文中提取XML的方法是什么?
application/json
和applicaiton/x-www-form-urlencoded
。您可以使用其他中间件来解析xml请求,like this one