Replies: 1 comment 6 replies
-
|
Hi @mestachs! I'm Dosu and I’m helping the superset team. Superset enforces CSRF protection on modifying API endpoints (like PUT to /api/v1/dataset/{id}) even when using Bearer JWT authentication, due to the @Protect() decorator on those routes. This means you must include a CSRF token in your requests, not just the Authorization header with your JWT token cite. To update a dataset via the API from Python, use requests.Session() to manage cookies, and follow this workflow:
Example: import requests
session = requests.Session()
# 1. Login
login_resp = session.post(
"https://your-superset/api/v1/security/login",
json={"username": "your_user", "password": "your_pass", "provider": "db"}
)
access_token = login_resp.json()["access_token"]
session.headers.update({"Authorization": f"Bearer {access_token}"})
# 2. Get CSRF token
csrf_resp = session.get("https://your-superset/api/v1/security/csrf_token/")
csrf_token = csrf_resp.json()["result"]
session.headers.update({"X-CSRFToken": csrf_token})
# 3. Update dataset
resp = session.put(
f"https://your-superset/api/v1/dataset/{dataset_id}",
json=dataset_payload
)
print(resp.json())Do not manually set the Cookie header; let requests.Session() handle it cite. If you get a 422 error from the CSRF endpoint about "Subject must be a string", make sure the 'sub' claim in your JWT is a string, not an integer cite. There is no supported way to disable CSRF for API endpoints using JWT Bearer tokens in Superset. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried something like this get the bearer token
then post the payload
but apparently the api complains about 400 Bad Request: The CSRF token is missing.
is there a way to get one ? (why is it asking for one since I use the Bearer JWT)
Beta Was this translation helpful? Give feedback.
All reactions