Serving an XML file from Glitch app


#1

Hey there,

I need to call a URL from a nodeJS function. The URL references an XML file that I’d like to store within my Glitch app. Can I store the XML file within my app and reference it with a local URL? I’ve been playing around with the Assets and public folders but can’t seem to get it to work.

Thanks!


#2

Hey, I think you can just upload the XML file to assets. Once uploaded you should be able to click on it and it should present you the option to copy the URL.

Is the issue that you can’t find the URL, or is it that you are having an issue referencing it?


#3

Hi @esouthren, welcome to the Glitch Forums!

Like @WenInCode I’ve been able to access an XML doc both in a public folder served statically by Express and via the Assets drawer without any difficulty - you can see it in https://glitch.com/edit/#!/anonymous-anaconda, there’s a public/api.xml file and one in Assets both of which are reachable in the browser at their expected URLs.

Can you give us a code snippet of how you’re trying to access it, or a project name that we can look at?


#4

Thanks for the reply!

Here’s the app:
https://glitch.com/edit/#!/twilio-quest-voice-example

This issue is line 37. I need to reference the URL of the XML file, rather than the XML itself.


#5

Oh I see. I think you are trying to get the URL from the XML file you uploaded to assets. I think you can do something similar to https://stackoverflow.com/a/41995792, where you parse the xml file to get the url in the Play section.

snippet from the linked stack overflow answer

fs = require('fs');
var parser = require('xml2json');

fs.readFile( './data.xml', function(err, data) {
    var json = parser.toJson(data);
    console.log("to json ->", json);
 });

Let me know if I am misunderstanding :blush:


#6

Thank you for the reply - but what I need is the URL of the whole XML file (I don’t know why I can’t pass the XML through the function :confused: ).

For context, if I use this URL - https://demo.twilio.com/docs/voice.xml - it works fine.

If I take the XML from the above URL and add it to my assets file as response.xml, and reference the full URL in my function - it doesn’t work. It can find it now, but the XML isn’t processed by Twilio (that could be a non-Glitch related error though).


#7

Update: When I try and use the assets/response.xml full URL, on the Twilio side of things I get a 405: Method Not Allowed error.

Their troubleshooting page advises:

* Make sure your web server allows HTTP POST requests to static resources (if the URL refers to .xml or .html files)


#8

That’s potentially useful from the Glitch side of things. What do you see when you host the xml file in your regular file system and server it via a public address (as opposed to in assets)?


#9

How do you reference the public address - https://twilio-quest-voice-example.glitch.me/response.xml, or something similar?


#10

Yep! That’s it exactly!

If it’s been more than 5 minutes since your last request you project may take a little longer to respond since it will have gone to sleep, so if it appears to be timing out you could try it again and see if the result is different.


#11

Aye, I just got a timeout - I’ll give it a few minutes then try again :slight_smile:


#12

Actually you might want to try again right away. The timeout might be because your project is waking up, and if you wait too long it will go back to sleep.

You can use UptimeRobot or something similar to keep it awake all the time if that’s a continual problem.


#13

Persistent timeout errors, unfortunately. I’ve tried half a dozen times in a row.

image

Code 11200 refers to a “HTTP retrieval error”.


#14

I get an identical error if I put a typo into the URL, so it’s perhaps an issue with the https://twilio-quest-voice-example.glitch.me/response.xml parameter?


#15

I don’t know much about Twilio; what are they trying to POST there? On closer examination I don’t think using a static url is going to allow that either.

Depending on what they require you may need to implement an endpoint they can POST to and then handle the resulting input appropriately. What’s the overall expected workflow supposed to look like?


#16

Not entirely sure what they’re posting.

Note that if the URL parameter is http://demo.twilio.com/docs/voice.xml, it works, but the assets/response.xml URL does not - even though they contain exactly the same XML data.