Skip to content

Commit

Permalink
RSS: set content type when writing HTTP response (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech authored Aug 28, 2023
1 parent 09f415a commit a94c961
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions feedhub/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"io"
"net/http"
"sort"
"time"
)
Expand Down Expand Up @@ -88,6 +89,16 @@ func ToXML(feed XmlFeed) (string, error) {
// WriteXML writes a feed object (either a Feed, AtomFeed, or RssFeed) as XML into
// the writer. Returns an error if XML marshaling fails.
func WriteXML(feed XmlFeed, w io.Writer) error {

// set Content-Type header based on feed type when using a ResponseWriter
rw, ok := w.(http.ResponseWriter)
if ok {
switch feed.(type) {
case *Rss:
rw.Header().Add("Content-Type", "application/rss+xml")
}
}

x := feed.FeedXml()
// write default xml header, without the newline
if _, err := w.Write([]byte(xml.Header[:len(xml.Header)-1])); err != nil {
Expand Down

0 comments on commit a94c961

Please sign in to comment.