Unable to requests.delete() from Python flask program
Dear All,
I am perfectly able to add domains to my allowlist programmatically via my Python flask rest API program:
import requests
import json
from flask import Flask, request
from flask_cors import CORS
app = Flask(__name__)
cors = CORS(app)
BASE_URL = "https://api.nextdns.io/profiles"
HEADERS = {
"Content-type": "application/json",
'x-api-key': "my_secret_api_token",
}
@app.route('/add_to_allowlist/<profile_id>', methods=['POST'])
def add_to_allowlist(profile_id):
domain = request.data.decode() # gives "googleapis.com"
post_dict = {
"id": domain,
"active": True,
}
response = requests.post(
f'{BASE_URL}/{profile_id}/allowlist', json=post_dict, headers=HEADERS)
print(response.content.decode("UTF-8"))
return response.content.decode("UTF-8")
However, when I try to delete an entry, I get {"errors":[{"code":"internalServerError"}]}. I have read many online documentation on how to write delete requests and have tried several attempts with the NextDNS API (changing URLs, payload format, header parameters, ...), but can not get it to work:
@app.route('/remove_from_allowlist/<profile_id>', methods=['DELETE'])
def remove_from_allowlist(profile_id):
domain = request.data.decode() # gives "googleapis.com"
post_dict = {
'id': domain,
"active": True,
}
response = requests.delete(
f'{BASE_URL}/{profile_id}/allowlist', json=post_dict, headers=HEADERS)
print(response.content.decode("UTF-8"))
return response.content.decode("UTF-8")
At this point, I'd appreciate any help :-) !
My allowlist under https://api.nextdns.io/profiles/profile_id/allowlist looks like this:
{"data":[{"id":"googleapis.com","active":true}]}
Looking forward to feedback; Thanks in advance for your time!
David
1 reply
-
I have been able to find the solution reading through other internet sources:
Since we want to update the content of the webpage https://api.nextdns.io/profiles/profile_id/allowlist, we can not use delete as this would delete the allowlist webpage. Instead, we want to update the allowlist and for this, we need to:- Fetch the current allowlist
- Build the updated allowlist on our own
- Upload the updated allowlist
@app.route('/get_allowlist/<profile>/<base_url>', methods=['GET']) def get_allowlist(profile, base_url): response = requests.get( f'{build_valid_url(base_url)}/{profile}/allowlist', headers=HEADERS) return response.content.decode("UTF-8") @app.route('/remove_from_allowlist/<profile>/<base_url>', methods=['PUT']) def remove_from_allowlist(password, base_url): domain = request.data.decode() print(f"Removal request from allowlist for {domain} on {profile}") allowlist= json.loads(get_allowlist(profile, base_url))["data"] new_allowlist = [] for entry in allowlist: if domain not in entry["id"]: new_allowlist.append(entry) response = requests.put( f'{build_valid_url(base_url)}/{profile}/allowlist', json=new_allowlist, headers=HEADERS) return response.content.decode("UTF-8")
Note that allowed methods have been changed to 'PUT' as well.
Content aside
- 12 days agoLast active
- 1Replies
- 37Views
-
1
Following