This guide shows you how to programmatically retrieve and update attributes for a generic RTSP resource using the REST API. You can perform these steps manually with an API testing client or automate them using a script.
Before making updates, ensure you have obtained a session token to authenticate your requests.
Retrieve current RTSP resource attributes
To update a resource, you must first identify its ID and check its current stream configuration. Query your Server to list all manually added Generic RTSP streams along with their IDs and active URLs.
To get a list of your generic RTSP devices:
Send a
GETrequest to the/rest/v4/devicesendpoint.Include the query parameters
vendor=GENERIC_RTSPand_with=name,id,streamUrlsto filter the results.Include your session token in the authorization header as a bearer token.
Request example
GET /rest/v4/devices?vendor=GENERIC_RTSP&_with=name,id,streamUrls HTTP/1.1
Host: localhost:7001
Authorization: Bearer <token>
Accept: application/jsonResponse example
The server returns an array of JSON objects containing the matching devices:
[
{
"id": "3a197fa3-64d7-ba3b-105b-4794254f5299",
"name": "Generic RTSP Camera 1",
"streamUrls": {
"1": "rtsp://localhost:8554/mh",
"2": ""
}
}
]Note the id of the resource you want to modify.
Update RTSP resource stream URLs
Once you have the target resource ID, you can add or modify its stream URLs.
To update a device's stream configuration:
Send a
PATCHrequest to the/rest/v4/devices/{id}endpoint, replacing{id}with your device's ID.In the request body, pass a JSON object containing the
streamUrlskey with your updated stream mapping.
Request example
PATCH /rest/v4/devices/3a197fa3-64d7-ba3b-105b-4794254f5299 HTTP/1.1
Host: localhost:7001
Authorization: Bearer <token>
Content-Type: application/json
{
"streamUrls": {
"1": "rtsp://localhost:8554/mh",
"2": "rtsp://localhost:8554/ml"
}
}This request adds a secondary low-resolution stream ("2") to the target device while maintaining or updating the primary stream ("1").
Automate updates with Python
You can automate this update process using the Python requests library. The following script updates the stream URLs for a specific device ID.
import requests
# Define your connection and resource details
server_url = "https://localhost:7001"
device_id = "3a197fa3-64d7-ba3b-105b-4794254f5299"
# Construct the API endpoint and payload
request_url = f"{server_url}/rest/v4/devices/{device_id}"
payload = {
"streamUrls": {
"1": "rtsp://localhost:8554/mh",
"2": "rtsp://localhost:8554/ml"
}
}
# Send the update request
response = requests.patch(
request_url,
auth=(username, password),
json=payload,
verify=False # Set to True if using a valid SSL certificate
)
# Verify the response
if response.status_code == 200:
print("Device streams updated successfully.")
print(response.json())
else:
print(f"Failed to update device. Status code: {response.status_code}")
print(response.text)
Authentication
Ensure you use an active session token to authenticate your REST API requests and scripts. For detailed instructions on how to obtain and manage your token, see the Authentication Support Article.
Comments
0 comments
Article is closed for comments.