You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell调用GitLab API上传PNG图片失败求助

GitLab API 上传PNG图片失败解决方法

问题场景

使用PowerShell调用GitLab API上传test.png时,执行以下代码后报错:

$token = "<my_access_token>"
$projectId = "5"
$file = "path\test.png"
$apiUrl = "https://gitlab.example.com/api/v4/projects/5/uploads"

Invoke-RestMethod -Uri $apiUrl -Method Post -Headers @{ "PRIVATE-TOKEN" = $token } -InFile $file -ContentType "image/png"

收到的错误信息:

Invoke-RestMethod : {"error":"The provided content-type 'image/png' is not supported."}

解决步骤

GitLab项目上传API要求请求以multipart/form-data格式提交,而非直接指定图片的Content-Type。调整代码如下:

  • 改用-Form参数构建multipart表单数据,将文件作为file字段传递
  • 无需手动指定ContentType,让Invoke-RestMethod自动处理表单的Content-Type

修正后的代码:

$token = "<my_access_token>"
$projectId = "5"
$filePath = "path\test.png"
$apiUrl = "https://gitlab.example.com/api/v4/projects/$projectId/uploads"

# 构建包含文件的表单数据
$formData = @{
    file = Get-Item -Path $filePath
}

Invoke-RestMethod -Uri $apiUrl -Method Post -Headers @{ "PRIVATE-TOKEN" = $token } -Form $formData

额外注意事项

  • 确认你的访问令牌已开启api权限(在GitLab个人设置的「访问令牌」页面配置)
  • 检查$filePath路径是否正确,确保PowerShell能找到目标图片文件
  • 若问题仍存在,可添加-Verbose参数运行命令,查看请求的详细日志以排查问题

内容的提问来源于stack exchange,提问作者Manu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:57:13