cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to use put method from python for environment API

hemanth
Observer

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

2 REPLIES 2

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!

"The lions does not ally with the coyotes"

Radoslaw_Szulgo
Inactive

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:

 

Spoiler
data = { "name": name, 
"id":vault_id,
"description":description,
"ownerAccessOnly":"false",
"type":"TOKEN",
"token":session_token
}

Then convert the payload to JSON format

Spoiler
json_payload = json.dumps(payload)

And then send the PUT request with the JSON payload

Spoiler
response = requests.put(url, data=json_payload, headers=headers)
Senior Product Manager,
Dynatrace Managed expert

Featured Posts