0

I have a django rest application back end with an angular front end. All the endpoints work perfectly locally but when I deploy it to google cloud all endpoints works except one which keeps on displaying the error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url (Reason: CORS request did not succeed). I have tried to connect the database on the cloud with the local application, all the endpoints requests are displayed on the IDE console except the one, and the browser displays the request is not yet finished. Any ideas on what is causing the issue will be highly appreciated. I have gone through several similar questions on stackoverflow and tried to implement the solutions provided but it is not working. All my other endpoints are working perfectly its only one which displays the error on google cloud app engine, connected to a PostgreSQL standard instance 1vcpu,3.75GB RAM. When I try with POSTMAN I get the error 502 Bad Gateway

Andrew
  • 105
  • 2
  • 10
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – The Fabio Apr 19 '22 at 01:20
  • For me all the other endpoints are working perfectly, without any issues, its only one which returns the CORS error on the browser and on postman it returns 502 Bad Gateway. But locally, it is working perfectly – Andrew Apr 19 '22 at 01:44
  • 1
    CORS is a server side config... it can be done in a per-endpoint basis. (per http verb on top of that) The answers to this other question have all the info you need to understand CORS and its effects. Postman does not implement CORS, so it would not happen there – The Fabio Apr 19 '22 at 05:10
  • Have you set CORS configuration for App Engine in http headers in the handlers section of the app.yaml file as mentioned [here](https://cloud.google.com/appengine/docs/standard/python/config/appref#handlers_element)? – Prabir Apr 20 '22 at 06:27

1 Answers1

1

Install Django-cors-headers to solve this problem Follow this link: https://pypi.org/project/django-cors-headers/

  1. python -m pip install django-cors-headers
  2. INSTALLED_APPS = [ ..., "corsheaders", ..., ] add middleware top of the commonmiddle
  3. MIDDLEWARE = [ ..., "corsheaders.middleware.CorsMiddleware", "django.middleware.common.CommonMiddleware", ..., ]
sibtain
  • 71
  • 3
  • I have already setup django-cors-headers, it is only one endpoint request which is causing the issues, all the other requests are working perfectly – Andrew Apr 20 '22 at 00:55