In this post I will show you how to enable CORS (Cross Origin Resource sharing) so that you can make HTTP requests from a different domain than the one you use for your Flask application written in Python.

We will just add a kind of middleware to modify all the responses and add the headers that allow CORS in Flask.

Enable Cross Origin Resource Sharing in Flask

Just add the following code, with the @app.after_request annotation to indicate that we are modifying the response after the request:

The Access-Control-Allow-Origin indicates the domain from which the requests will be allowed. Credentials is useful in case you use sessions or cookies. Access-Control-Allow-Methods indicates which HTTP methods are valid for CORS.

Finally Access-Control-Allow-Headers indicates which headers will be accepted for CORS.

Remember that you are free to modify any value to adapt it to your needs.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

Suscribir por correo

Ingresa tu correo y recibirás mis últimas entradas sobre programación, open source, bases de datos y todo lo relacionado con informática

Join 4,207 other subscribers


parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.

%d bloggers like this: