.netangularhttp-status-code-415

JSON.stringify API returns 415 (.net & angular)


I'm running into a bit of trouble on a POST request from my angular front to my .net API.

Here is the code on the front:

[...]       
this.uploadFile(JSON.stringify(jsonobject));
}

uploadFile(file)
{
    this.files.push(file);
    this.uploadService.uploadjsonresponse(file, "JsonFilterResponse").subscribe();
}

and the post request:

public uploadjsonresponse(file, apiname) {
     return this.httpClient.post(this.SERVER_URL + apiname, file);  
}

and finally the .net code:

[HttpPost]
[Route("api/JsonFilterResponse")]
[RequestSizeLimit(4_000_000_000)]
[Microsoft.AspNetCore.Cors.EnableCors]
public IActionResult JsonFilterResponse([FromBody] string jsonresult)
{
    try
    {
        if (jsonresult != null)
        {
            Response.Body.Flush(); // http response
            return Ok(); // server response
        }
        else
            return NotFound();

    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        return StatusCode(500, "Internal server error");
    }
}

The JSON object is correct (checked on the console) and so is the API address. The API is returning a 415 that I can't seem to shake. Any suggestions would be much appreciated!


Solution

  • To those who may be wondering, I found the answer! Replace:

    [FromBody] string jsonresult
    

    with

    [FromBody] JsonElement jsonresult
    

    and use

    var json = System.Text.Json.JsonSerializer.Serialize(jsonresult);
    

    to interpret the result!