0

I have the following code that works and I get the file output.txt. I would like for the output file to say success when it works and to provide error code when it doesn't.

import requests
import json
f = open('output.txt', 'w')
url = 'https://webapi.teamviewer.com/api/v1/account'
payload = {'name': 'alias', 'email': 'user@teamviewer.com'}
headers = {"content-type": "application/json", "Authorization": "Bearer myuser token"}
r = requests.put(url, data=json.dumps(payload), headers=headers)
f.write(r.text)
f.close()

TeamViewer HTTP Response codes are:

200 – OK: Used for successful GET, POST and DELETE. 204 – No Content: Used for PUT to indicate that the update succeeded, but no content is included in the response. 400 – Bad Request: One or more parameters for this function is either missing, invalid or unknown. Details should be included in the returned JSON. 401 – Unauthorized: Access token not valid (expired, revoked, …) or not included in the header. 403 – Forbidden / Rate Limit Reached: IP blocked or rate limit reached. 500 – Internal Server Error: Some (unexpected) error on the server. The same request should work if the server works as intended.

Alfonso Jr
  • 39
  • 8

1 Answers1

1

You can get the result and error code from your response (assuming TeamViewer api is well behaved):

r = requests.put(url, data=json.dumps(payload), headers=headers)
if r.status_code == 200:
   f.write('success')
else
   f.write('{0}: {1}'.format(r.status_code, r.text))
Fernando Cezar
  • 858
  • 7
  • 22