GitLab API创建远程镜像时无法识别含%20的URL地址
GitLab API创建含%20的远程镜像返回url为null的问题原因
问题背景
使用GitLab API v4创建远程镜像时,当目标URL包含URL编码空格%20,执行以下curl命令:
curl --request POST --data 'url=https://user:password@remotegit.com/Repo%20Mirror%20Test/_git/Repo%20Mirror%20Test' --data "enabled=true" --header "Authorization: Bearer TOKEN" "https://mygit.com/api/v4/projects/171/remote_mirrors"
返回结果中url字段为null;但无特殊字符的URL可正常创建,且通过GitLab UI使用相同URL能成功创建镜像,调整curl引号后问题依旧。GitLab服务器版本为15.2.2。
问题原因
这是GitLab 15.2.x版本的API解析bug:
- 当通过表单键值对(
--data方式)发送请求时,API会对URL参数进行二次URL解码,将%20还原为普通空格; - GitLab的远程镜像URL校验逻辑不支持带普通空格的地址,因此校验失败后返回
url: null。
而GitLab UI能正常工作的原因是,UI发送请求时将URL放在JSON payload中传递,而非表单数据,避免了二次解码的问题。
解决办法
- 改用JSON格式发送请求,示例curl命令:
curl --request POST \ --header "Authorization: Bearer TOKEN" \ --header "Content-Type: application/json" \ --data '{ "url": "https://user:password@remotegit.com/Repo%20Mirror%20Test/_git/Repo%20Mirror%20Test", "enabled": true }' \ "https://mygit.com/api/v4/projects/171/remote_mirrors"
- 升级GitLab版本:该bug在15.3及以上版本中已被修复,升级后可继续使用表单数据方式请求。
内容的提问来源于stack exchange,提问作者Daniel Procházka
相关产品推荐
相关产品推荐

