I'm trying to retrieve an xml podcast feed in my application. I'm having a problem where one of the fetch
requests I'm making returns an html document instead of the xml feed. The weird part is if I send a /GET
to the same url with Postman, it returns me the rss feed. It also appears to work with the code snippet here on SO (example below).
An example of it returning a html document can be seen at this fiddle, which has the exact same code as the snippet below.
Is anyone able to help me understand why this is happening please? I feed like it could be resolved by setting some sort of request header, maybe?
Update Weirdly, it seems like the linked fiddle sometimes fetches the xml; I think because it caches the calls. If you open it in a new incognito window, it fetches the html again.
$('#fetchXML').click(function(){
$('#xmlContent').text('Loading ...');
const feedUrl = 'https://cors-anywhere.herokuapp.com/http://feeds.wnyc.org/dearhankandjohn';
const headers = new Headers({
'X-Requested-With':'XMLHttpRequest'
});
fetch(feedUrl, headers)
.then(function(response) {
return response.text();
})
.then(function(xmlText) {
$('#xmlContent').text(xmlText);
})
.catch(function(err){
$('#xmlContent').text(err);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><button id="fetchXML">
Fetch Feed
</button></td>
</tr>
<tr>
<td id="xmlContent"></td>
</tr>
</table>
TL;DR: Pass the format
as a parameter in the URL.
https://feeds.feedburner.com/codinghorror?format=html
Feedburner returns raw XML by default:
HTTP/1.1 200 OK
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"
Cache-Control: private, max-age=0
Content-Encoding: gzip
Content-Type: text/xml; charset=UTF-8
But it also sends a stylesheet so the browser can apply style the markup (view source).
Time ago, the deprecated, fmt
parameter was used, now the with format
you can ask either for html
or xml
.
https://feeds.feedburner.com/codinghorror?format=html
HTTP/1.1 200 OK
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"
Cache-Control: private, max-age=0
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8