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

通过Confluence REST API上传附件时遇Error 415问题求助

解决Confluence Server REST API上传附件415 Unsupported Media Type错误

问题核心

你用PowerShell的Invoke-RestMethod上传附件时遇到415错误,本质是请求格式不符合Confluence API的要求——Confluence要求上传附件必须使用multipart/form-data格式的表单请求,且需要正确包含文件字段和必要请求头。

错误原因分析

  1. 直接使用-InFile参数时,PowerShell会将文件内容作为原始请求体发送,而非Confluence需要的multipart表单格式,导致服务器无法识别媒体类型。
  2. 手动指定multipart/form-data但未提供正确的边界(boundary),服务器无法解析请求体。
  3. 错误设置Content-Type(如application/json、text等),不符合API要求的表单格式。

通用解决方案(支持txt/docx等多类型文件)

使用MultipartFormDataContent构建符合要求的multipart请求,自动处理边界和Content-Type,同时添加必要的请求头:

# 配置参数
$pageId = "<你的页面ID>"
$confluenceBaseUrl = "https://<confluence服务器地址>/confluence"
$filePath = "file.txt" # 替换为你的文件路径,支持docx等类型
$basicAuth = "<Base64编码的用户名:密码>" # 可通过 [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("用户名:密码")) 生成

# 构建multipart表单内容
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()

# 添加文件流及对应Content-Type
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
$fileContent = [System.Net.Http.StreamContent]::new($fileStream)
$fileExt = [System.IO.Path]::GetExtension($filePath).ToLower()

# 根据文件类型设置正确的Content-Type
switch ($fileExt) {
    ".txt" { $fileContent.Headers.ContentType = "text/plain" }
    ".docx" { $fileContent.Headers.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
    ".pdf" { $fileContent.Headers.ContentType = "application/pdf" }
    ".jpg" { $fileContent.Headers.ContentType = "image/jpeg" }
    # 可根据需要添加更多文件类型映射
}

# 将文件添加到表单,字段名必须为"file"(Confluence API要求)
$multipartContent.Add($fileContent, "file", [System.IO.Path]::GetFileName($filePath))

# 配置请求头
$headers = @{
    "Authorization" = "Basic $basicAuth"
    "X-Atlassian-Token" = "nocheck" # 必须添加,绕过Confluence的CSRF检查
}

# 发送请求
try {
    $uploadUrl = "$confluenceBaseUrl/rest/api/content/$pageId/child/attachment?allowDuplicate=true" # allowDuplicate=true允许重复上传
    $response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -Body $multipartContent
    Write-Host "上传成功:`n$($response | ConvertTo-Json -Depth 10)"
} catch {
    Write-Host "上传失败:$($_.Exception.Message)"
    if ($_.Exception.Response) {
        $errorStream = $_.Exception.Response.GetResponseStream()
        $reader = [System.IO.StreamReader]::new($errorStream)
        $errorDetails = $reader.ReadToEnd()
        Write-Host "错误详情:`n$errorDetails"
    }
}

关键说明

  • 表单字段名:必须使用file作为文件字段的名称,这是Confluence REST API的强制要求。
  • X-Atlassian-Token:该请求头用于绕过Confluence的CSRF防护机制,上传附件时必须添加,否则会被服务器拦截。
  • Content-Type自动匹配:根据文件扩展名设置对应的媒体类型,确保Confluence能正确识别并处理文件(比如docx对应的application/vnd.openxmlformats-officedocument.wordprocessingml.document)。
  • 避免手动设置全局Content-Type:MultipartFormDataContent会自动生成包含边界的multipart/form-data请求头,无需手动指定,否则会导致边界缺失错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 08:47:49