Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS Headers Issue #41

Open
ibrahimyaacob92 opened this issue Jun 28, 2021 · 1 comment
Open

CORS Headers Issue #41

ibrahimyaacob92 opened this issue Jun 28, 2021 · 1 comment

Comments

@ibrahimyaacob92
Copy link

Problem with CORS as mentioned below.

image

@craigahobbs
Copy link

craigahobbs commented Jul 2, 2024

It's been two years since I submitted my pull request to fix this issue. I never received a response, so I'm deleting it. It would be interesting to get a response from @crflynn as to why. It seems like a no-brainer to me.

Here's the diff of the fix if anyone wants to revive it.

diff --git a/pypistats/views/api.py b/pypistats/views/api.py
index 6ec8a2b..2878ed5 100644
--- a/pypistats/views/api.py
+++ b/pypistats/views/api.py
@@ -16,6 +16,13 @@ from pypistats.models.download import SystemDownloadCount
 blueprint = Blueprint("api", __name__, url_prefix="/api")
 
 
+def jsonify_with_cors_headers(response):
+    """JSON-ify a response and add CORS headers"""
+    response_jsonified = jsonify(response)
+    response_jsonified.headers["access-control-allow-origin"] = "*"
+    return response_jsonified
+
+
 @blueprint.route("/")
 def api():
     """Get API documentation."""
@@ -47,7 +54,7 @@ def api_downloads_recent(package):
     else:
         abort(404)
 
-    return jsonify(response)
+    return jsonify_with_cors_headers(response)
 
 
 @blueprint.route("/packages/<package>/overall")
@@ -82,7 +89,7 @@ def api_downloads_overall(package):
     else:
         abort(404)
 
-    return jsonify(response)
+    return jsonify_with_cors_headers(response)
 
 
 @blueprint.route("/packages/<package>/python_major")
@@ -120,7 +127,7 @@ def generic_downloads(model, package, arg, name):
     else:
         abort(404)
 
-    return jsonify(response)
+    return jsonify_with_cors_headers(response)
 
 
 # TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants