我正在尝试在 Vapor 中使用 XML 正文解码请求。
请求内容为:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns="http://www.w3.org/2005/Atom"><link rel="hub" href="https://pubsubhubbub.appspot.com"/><link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=REDACTED"/><title>YouTube video feed</title><updated>2024-11-27T15:08:44.561926689+00:00</updated><entry>
<id>yt:video:REDACTED</id>
<yt:videoId>REDACTED</yt:videoId>
<yt:channelId>REDACTED</yt:channelId>
<title>Test Video</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=REDACTED"/>
<author>
<name>Test User</name>
<uri>https://www.youtube.com/channel/REDACTED</uri>
</author>
<published>2024-11-27T15:08:41+00:00</published>
<updated>2024-11-27T15:08:44.561926689+00:00</updated>
</entry></feed>
我正在尝试将其解码为以下模型:
struct PubSub: Content {
struct Feed: Codable {
struct Entry: Codable {
let id: String
}
let entry: Entry
}
let feed: Feed
}
使用以下代码进行解码:
guard let pubSub = try? req.content.decode(PubSub.self, as: .xml) else {
throw Abort(.badGateway)
}
return pubSub
由于某种原因,它永远无法成功解码。有人可以分享 Vapor 解码 XML 的正确方法吗?