I'm trying to decode a request with XML body in Vapor.
The request content is:
<?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>
I am trying to decode it into the following model:
struct PubSub: Content {
struct Feed: Codable {
struct Entry: Codable {
let id: String
}
let entry: Entry
}
let feed: Feed
}
Using the following code to decode it:
guard let pubSub = try? req.content.decode(PubSub.self, as: .xml) else {
throw Abort(.badGateway)
}
return pubSub
For some reason it never succeeds to decode. Could someone share what the correct way of decoding XML is for Vapor?
Vapor does not have an inbuilt decoder for XML so you'd need to find one that works for you. Once you have one you can conform it to ContentDecoder
and register it as per the docs