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

ASP.NET Core Web API上传图片出现415不支持媒体类型错误排查

415 Unsupported Media Type错误的可能原因分析

我尝试调用POST服务上传png、jpg格式图片,但通过Postman测试(实际经另一API调用目标服务)时,出现415 Unsupported Media Type错误。错误响应信息如下:

{StatusCode: 415, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { X-AREQUESTID: 1196x14887183x1 X-ANODEID: node2 X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN Content-Security-Policy: frame-ancestors 'self' X-ASEN: SEN-17699581 Set-Cookie: JSESSIONID=4548966F3B9BAFC283932093C14B6CF7; Path=/; Secure; HttpOnly Set-Cookie: atlassian.xsrf.token=BYCQ-GVBW-0E9H-MNTX_18760f22f63318f1c8b814586ccf9952dcfe353d_lin; Path=/; Secure X-Seraph-LoginReason: OK X-ASESSIONID: 1amnj3o X-AUSERNAME: glgarcia Date: Wed, 09 Nov 2022 23:56:45 GMT Content-Type: text/html; charset=UTF-8 Content-Length: 0 }}

Web API端代码如下:

public async Task<IActionResult> CreateImagenesQuejasOpinionesSugerencia([FromForm] QuejasSugerenciaOpinionesImagenesRequest quejasImagenesRequest,string issueKey)
{
    QuejasSugerenciaOpinionesImagenesResponse.Root quejasImagenesResponse = new QuejasSugerenciaOpinionesImagenesResponse.Root();
    //return System.Convert.ToBase64String(textAsBytes);
    try
    {
        using (var client = new HttpClient())
        {
            // Setting Base address.  
            client.BaseAddress = new Uri($"https://gestiondecasos.humano.local/");
   
            // Initialization.  
            HttpResponseMessage response = new HttpResponseMessage();
            // HTTP POST  
            var imagenGuardo = quejasImagenesRequest.ConvertToBase64();
            response = await client.PostAsJsonAsync($"rest/api/2/issue/{issueKey}/attachments", imagenGuardo).ConfigureAwait(false);

            // Verification  
            if (response.IsSuccessStatusCode)
            {
                // Reading Response.  
                var result = await response.Content.ReadAsStringAsync();
                quejasImagenesResponse = JsonConvert.DeserializeObject<QuejasSugerenciaOpinionesImagenesResponse.Root>(result);
            }
        }
        return Ok(quejasImagenesResponse);
    }
    catch (HttpRequestException ex)
    {
        return Ok(ex.Message);
    }
    finally
    {
    }
}

错误的核心可能原因:

  • 请求媒体类型与目标API要求不匹配
    目标是Jira的附件上传接口,该接口要求用multipart/form-data格式上传文件,但代码中使用PostAsJsonAsync方法会将内容序列化为JSON,自动设置Content-Type为application/json,这与Jira接口的要求完全冲突,直接触发415错误。

  • 文件传输方式不符合接口规范
    代码将图片转成Base64字符串后通过JSON发送,但Jira附件接口不接收这种格式,它需要直接上传二进制文件内容,并封装在multipart/form-data的表单字段中。

  • 缺失关键请求头
    Jira的附件上传接口需要特定请求头支持:

    • 必须添加X-Atlassian-Token: no-check绕过XSRF检查(从响应头的atlassian.xsrf.token可看出目标服务启用了该防护);
    • 使用multipart/form-data时,需确保表单字段名符合Jira要求(通常为file字段,对应文件二进制内容)。
  • Postman测试环节的潜在问题
    如果Postman是通过你的Web API间接调用目标服务,问题根源在API代码;如果是直接调用目标服务出错,可能是Postman中Content-Type设置错误,或者未正确配置multipart/form-data的文件参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:10:27