API working through cURL but not python

For Workers & Pages, what is the name of the domain?

Example.com

What is the error number?

7003

What is the error message?

ould not route to /client/v4/accounts/None/storage/kv/namespaces/NAME_SPACE/values/KEY_ID perhaps your object identifier is invalid?

What is the issue or error you’re encountering

Calling the api by cURL works fine, calling it by urllib3 or requests results in 7003, same exact data , also Cloudflare SDK is not authenticating the token, but it works in cURL & urllib3 & requests

What steps have you taken to resolve the issue?

Google, Stackoverflow, Cloudflare Docs, this Community topics

What are the steps to reproduce the issue?

update a existing key in kv db with an api call
use code sample from api docs, but with api_token not api_key , cURL will work flawlessly, but if you use python urllib3 or requests it will authenticate but fail with no route to key , if you try to use cloudflare python sdk it will fail with this error cloudflare.BadRequestError: Error code: 400 - {‘success’: False, ‘errors’: [{‘code’: 9106, ‘message’: ‘Missing X-Auth-Key, X-Auth-Email or Authorization headers’}]}, same token that is woring in cURL,

Got it to work with urllib3 , for future if anyone facing the content-type issue just set to empty string

headers = {
        "Content-Type": "",
        "Authorization": f"Bearer {CLOUDFLARE_API_TOKEN}",
    }```

Full code

import os
import dotenv
import urllib3


def add_new_kv_urllib(key_id):
    dotenv.load_dotenv()
    CLOUDFLARE_API_TOKEN=os.getenv("CLOUDFLARE_API_TOKEN")
    CLOUDFLARE_ACCOUNT_ID=os.getenv("CLOUDFLARE_ACCOUNT_ID")
    NAMESPACE_ID=os.getenv("NAMESPACE_ID")
    http = urllib3.PoolManager()
    url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/storage/kv/namespaces/{NAMESPACE_ID}/values/{key_id}"
    headers = {
        "Content-Type": "",
        "Authorization": f"Bearer {CLOUDFLARE_API_TOKEN}",
    }
    data = "01J9KM5WQQST2HJZAKRNWKD4R4"

    response = http.request(
        "PUT",
        url,
        headers=headers,
        body=data,
    )
    is_success = response.json().get("success")
    print(is_success)
    print(response.status)

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.