Nextcloud Tables链接列URL显示不全问题求助
问题:Nextcloud Tables API插入链接类型字段时URL被截断
原本开发的应用通过Nextcloud Tables API插入数据功能正常,但将“Backup Link”“Restore Link”“Evidence Link”三列从文本类型改为链接类型后,出现URL截断问题。发送的完整URL如https://nextcloud.example.com/1/asjklDHBkauBS,在Nextcloud中仅显示https://nextcloud.example.c/,已确认发送的URL内容本身正确。
简化后的代码
import requests import json import datetime import logging NC_BASE_URL = "https://nextcloud.example.com" TABLES_API_BASE = f"{NC_BASE_URL}/apps/tables/api/1/tables" def get_column_mapping(table_id, auth_config): """Retrieve the mapping between column names and their IDs.""" try: auth = auth_config['auth'] headers = auth_config['headers'] response = requests.get(f"{TABLES_API_BASE}/{table_id}/columns", headers=headers, auth=auth, verify=auth_config['verify'], timeout=auth_config['timeout']) if response.status_code == 200: columns = response.json() mapping = {col['title'].strip(): str(col['id']) for col in columns} return mapping else: print(f"Error retrieving columns: {response.status_code} - {response.text}") return None except Exception as e: logging.error(f"Error connecting to Nextcloud: {e}") return None def register_backup(nc_user, nc_credential, auth_config, table_id, link_backup, link_restore, link_evidence, file_name): """Insert a new row into a Nextcloud Tables table.""" mapping = get_column_mapping(table_id, auth_config) if not mapping: return False current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") payload_data = { mapping.get("Date"): current_date, mapping.get("Backup Link"): link_backup, mapping.get("Restore Link"): link_restore, mapping.get("Evidence Link"): link_evidence, mapping.get("FileName"): file_name, } payload_data_clean = {k: v for k, v in payload_data.items() if k and v is not None and v != ""} payload = {"data": payload_data_clean} print(json.dumps(payload, indent=2, ensure_ascii=False)) try: response = requests.post(f"{TABLES_API_BASE}/{table_id}/rows", json=payload, **auth_config) if response.status_code in [200, 201]: logging.info(f"Backup successfully registered: {file_name}") return True else: logging.error(f"Error registering backup ({response.status_code}): {response.text}") return False except Exception as e: logging.error(f"Connection error: {e}") return False
解决方案
问题根源在于链接类型字段的API数据格式要求与文本类型不同:文本类型直接传入字符串即可,但链接类型需要传入包含url键的JSON对象,而非纯字符串。当直接传字符串时,Nextcloud Tables API会尝试解析不完整的格式,导致URL被截断。
修改代码中构造payload的部分,将链接字段的值从字符串改为包含url的对象:
payload_data = { mapping.get("Date"): current_date, mapping.get("Backup Link"): {"url": link_backup}, mapping.get("Restore Link"): {"url": link_restore}, mapping.get("Evidence Link"): {"url": link_evidence}, mapping.get("FileName"): file_name, }
修改后按以下步骤验证:
- 通过
print(json.dumps(payload, indent=2, ensure_ascii=False))确认payload中的链接字段格式为{"url": "完整URL"} - 发送请求后检查Nextcloud Tables中的数据,确认URL完整显示
内容的提问来源于stack exchange,提问作者alejandro jimenez
相关产品推荐
相关产品推荐

