The following urllib code works to connect to the using the :
import urllib3
import json
# Initialize a PoolManager instance
http = urllib3.PoolManager()
# Define the URL, headers, and data
url = "https://<url>"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json; charset=utf-8"
}
data = {
"name": "some_test"
}
# Encode data to JSON
encoded_data = json.dumps(data).encode('utf-8')
# Send the POST request
response = http.request(
'POST',
url,
body=encoded_data,
headers=headers
)
# Print the response status and data
print(response.status)
print(response.data.decode('utf-8'))
I also tried connecting via this CURL command which works:
curl -X "POST" "https://<url>"
-H 'Authorization: Bearer <token>'
-H 'Content-Type: application/json; charset=utf-8'
-d $'{
"name": "some_test"
}'
But using the requests module the code returns a 401 message -> Unauthorized:
import requests
url = "<url>"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json; charset=utf-8"
}
data = {
"name": "some_test"
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.text)
I tried different requests version but all behave the same. What am I missing? Could it be a different conflict?