SharePoint REST API问题:含空格列表名无法通过getByTitle获取
以下是适配动态工具需求的可行方案:
- 替换空格为%20而非依赖URLEncoder.encode
URLEncoder.encode会将空格转为+,但SharePoint REST API在getbytitle()的单引号参数内部仅识别%20作为空格的转义符。直接替换空格即可:
String listName = "TWO WORDS"; String encodedListName = listName.replace(" ", "%20"); String requestUrl = siteUrl + "/_api/web/lists/getbytitle('" + encodedListName + "')/items";
- 扩展处理特殊字符
如果列表名称包含&、#这类特殊字符,可针对性替换为对应URL编码,同时保留单引号包裹格式:
String listName = "TWO WORDS & TEST#1"; String encodedListName = listName.replace(" ", "%20") .replace("&", "%26") .replace("#", "%23"); String requestUrl = siteUrl + "/_api/web/lists/getbytitle('" + encodedListName + "')/items";
- 检查URL格式细节
确保请求URL满足以下要求:- 使用英文单引号包裹列表名称,不要用中文单引号
- 拼接URL时没有引入多余转义或空格
- 站点URL末尾无多余斜杠,避免拼接后出现
//格式错误
内容的提问来源于stack exchange,提问作者Jeries Haddad
相关产品推荐
相关产品推荐

