简体   繁体   中英

API returning unsupported media type in ASP.NET Core

I have the following API when I do a request to it, it returns unsupported media type I have no idea what is causing this problem.

[HttpPost("select/manager/{projectId}")]
public async Task<ActionResult<string>> SetProjectManager([FromBody] string name, string projectId)
{
    var (isValid, username, password) = spManager.GetCredentials(HttpContext.Request.Headers["x-token"]);

    if (!isValid || string.IsNullOrEmpty(name))
    {
        return BadRequest("Bad Key!");
    }

    var result = await spManager.SetProjectManager(username, password, name, projectId);

    if (result.Success == true)
    {
        return Ok(new SubmissionResponse { Success = true, ErrorCode = null });
    }
    else
    {
        return NotFound(new SubmissionResponse { Success = false, ErrorCode = "Not Found!" });
    }
}

I am sending a token in the header but it's not shown in the screenshot:

在此处输入图像描述

replace "cristof" with {"name": "cristof"} and change the body type to Json.

when I do a request to it, it returns unsupported media type

The HTTP 415 Unsupported Media Type error indicates that the server refuses to accept the request because the payload format is in an unsupported format. And please note that it doesn't provide a built-in input formatter for plain text in ASP.NET Core Web APIs.

The framework provides built-in input and output formatters for JSON and XML. To make the request with your data work well, as @Nkosi suggested, you can try to change the Content-Type to application/json .

在此处输入图像描述

Besides, if you really want your APIs work well with plain text input, you can try to implement a custom plain text input formatter.

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/custom-formatters?view=aspnetcore-5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM