POST and PUT Syntax in Python
Hello Everyone,
I'm currently working on a project combining generating RAPID code with ChatGPT and sending it via Robot Web Services to GoFa.
I'm working in Python.
So far I've been able to establish connection and use GET method correctly.
However I receive Forbidden Request errors(403) while trying to use POST.
Also I'm looking for PUT Syntax(especially for Uploading a file) since "Execute" button doesn't seem to work correctly on DeveloperCenter site:
https://developercenter.robotstudio.com/api/RWS
Here's my code so far.
Could You please let me know what's wrong with it?
I'm currently working on a project combining generating RAPID code with ChatGPT and sending it via Robot Web Services to GoFa.
I'm working in Python.
So far I've been able to establish connection and use GET method correctly.
However I receive Forbidden Request errors(403) while trying to use POST.
Also I'm looking for PUT Syntax(especially for Uploading a file) since "Execute" button doesn't seem to work correctly on DeveloperCenter site:
https://developercenter.robotstudio.com/api/RWS
Here's my code so far.
Could You please let me know what's wrong with it?
from aiohttp.hdrs import CONTENT_TYPE
from requests.auth import HTTPBasicAuth
import logging
import requests
import json
# Konfiguracja autoryzacji
basic_auth = HTTPBasicAuth("Default User", "robotics")
# Ustawienie formatowania logów
FORMAT = '%(asctime)-15s%(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
logger = logging.getLogger('RWS')
# Nagłówki
get_headers = {'Accept': 'application/hal+json;v=2.0'}
post_headers = {'Accept': 'application/json',
'Content-Type':'application/x-www-form-urlencoded;v=2.0'}
# Funkcja do wykonywania zapytań HTTP
def send_request(conn, method, uri, headers=None, data=None, proto='https://', host='192.168.125.1:443'):
url = f"{proto}{host}{uri}"
try:
# Wykonanie żądania HTTP
resp = conn.request(method, url, auth=basic_auth, headers=headers, data=data, verify=False)
logger.info(f"{method} {url} - Status Code: {resp.status_code}")
if resp.status_code == 200:
logger.info(f"Response: {resp.json()}")
else:
logger.error(f"Error: Failed to access URI '{uri}', Status Code: {resp.status_code}")
return resp
except Exception as e:
# Logowanie błędów
logger.error(f"Error: {e}")
return None
if __name__ == '__main__':
# Utworzenie sesji
conn = requests.Session()
conn.cookies.clear() # Upewnienie się, że ciasteczka są czyste przed logowaniem
# Wykonanie żądania GET
send_request(conn, 'GET', '/ctrl/clock', headers=get_headers)
# Wykonanie żądania POST
send_request(conn, 'POST', '/rw/rapid/execution/resetpp?mastership=explicit', headers=post_headers)
# Zamknięcie sesji
conn.close()
Tagged:
0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 298 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 316 IRC5
- 63 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 801 RAPID Programming
- 1 AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings