02 Jan 2024 04:01 PM - edited 02 Jan 2024 04:04 PM
Hello,
I am unable to use put method in dynatrace from python requests module, My code is as follows, i am trying to update the credential vault entry it throws the below error,
{'error': {'code': 400, 'message': 'Syntax error near line 1 column 6'}}
Seems like there is an issue with ":"
def put_authtoken_in_dt(vault_id, session_token, name, description):
url = "https://<host>/e/<env-id>/api/v2/credentials/"+ vault_id
print(url)
headers = {
"accept": "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Api-Token <token>"
}
data = {"name": name,
"id":vault_id,
"description":description,
"ownerAccessOnly":"false",
"type":"TOKEN",
"token":session_token
}
try:
response = requests.put(url, headers=headers, data=data ,verify=False)
print(response.request.body)
if response.status_code < 400:
print(response.json())
return True
else:
print(response.json())
return False
except Exception as e:
print(e)
return False
Please let me if i am doing something wrong in request headers.
Thanks
Solved! Go to Solution.
03 Jan 2024 09:52 AM
Hi!
Why you put in authorization
"Api-Token <token>"
token in <> brackets. I don't know if this is a case but maybe try:
DT_API_TOKEN=""
"Authorization": "Api-Token " + DT_API_TOKEN
Have a nice day!
03 Jan 2024 10:49 AM
IMHO the error suggests it's a payload issue, rather than authorization issue.
If I were you, I'd try with the other method to create a payload object. For example:
data = { "name": name,
"id":vault_id,
"description":description,
"ownerAccessOnly":"false",
"type":"TOKEN",
"token":session_token
}
Then convert the payload to JSON format
And then send the PUT request with the JSON payload