Forums

Django API with CORS enabled: access is being denied after the app has run for some days, forcing me to restart it

I'm building a simple API which is to be used with a web application. It receives an image, processes it and responds with some JSON data. I'm setting "CORS_ORIGIN_ALLOW_ALL = True" in the Django config to enable access from an external website.

When I reload my web app by clicking on the green reload button it will work for some time (about a week) and then it will start failing, responding with a 502 error, saying: "cannot load [mywebsite].pythonanywhere.com. No 'Access-Control-Allow-Origin' header is present on the requested resource". If I click the green reload button again it will work again for a couple of days.

I'd be thankful for any help!

That's very odd. Do you have a specific URL that you're trying to access from the other website that you could share?

The other website is sending a post request containing an image file to http://markoknoebl2.pythonanywhere.com/edgetrace/

It's working without any error at the moment

Ah, right -- I didn't realise it was a post. I think the best thing to do is to investigate what headers are coming back in the response when it's not working. Could you copy them from the browser developer console next time it happens (let me know if you need instructions on how to do that) and either paste them here or send them to us via user feedback?

So, apparently the issue occured again yesterday. You can see the HTTP headers here: http://i.imgur.com/QBfzDv8.png However, this time the server apparently got back to a working state without any action on my side (i.e. no restart necessary)

That looks like your web app was taking a while to restart. We shut down web apps after 26 hours of inactivity. If your web app takes a long time to respond to the first request, you'd get that error. It doesn't look like the particular error that you showed us has anything to do with CORS.

Ok, so apparently this has nothing to do with CORS.

So do you think that the error occurs after the web app has been inactive for a long time? The app will indeed take a long time to respond to a request (processing time is usually between a few seconds and a minute)

Any suggestions on how to avoid this issue?