Not able to purge cache with python

Hello all!
I’m trying to automate some routine work, so I decided to use Python’s requests library.
The main idea is to make possible to purge cache. Interesting, that I’m able to do it with curl request, but with Python I’m getting error:

400 Client Error: Bad Request for url:

My curl request, that works:

curl -X POST “
-H “Content-Type:application/json”
-H “Authorization: Bearer tokenID”
–data ‘{“hosts”: ["", “”]}’

My python code, that doesn’t work:

def prepare_request_body(domains: list) → dict:
return {“hosts”: domains}

def prepare_headers(cloudflare_token: str) → dict:
return {
“Content-type”: “application/json”,
“Authorization”: "Bearer " + cloudflare_token

def generate_cloudflare_url(prefix: str, zone_id: str, suffix: str):
return prefix + ‘/’ + zone_id + ‘/’ + suffix

def make_request(url: str, headers: dict, body: dict) → dict:
response =
return {
“error”: False,
“message”: “Request finished”,
“response”: response.text,
“status_code”: response.status_code

For debug information, I’ve printed generated headers and body:

Headers: {‘Content-type’: ‘application/json’, ‘Authorization’: ‘Bearer tokenHere’}
Body: {‘hosts’: [’’, ‘’]}

What’s wrong with my code?

P.S. Sorry for unformatted text - no possibility to format correctly code blocksPreformatted text