C#C
C#3y ago
Sarco

✅ [JsonProprety] isn't being respected ASP.NET Core

Howdy! I've been having this issue that I can't get figure out.

For context this is a ASP.NET Core WebAPI

I have only one controller which is causing me issues and I can't quite figure out why.

This is the signature for it:
[HttpPost]
public async Task<IActionResult> Post([FromForm] PostRequest request, [FromForm] IFormFile? file)
{
  ...
}


This is how PostRequest class looks like:
using Newtonsoft.Json;

public class PostRequest
{
    [JsonProperty("stamps")]
    public bool TimeStamps { get; set; }

    [JsonProperty("lang")]
    public string Lang { get; set; } = "auto";

    [JsonProperty("translate")]
    public bool Translate { get; set; }

    [JsonProperty("model")]
    public string Model { get; set; } = "base";
}


The issue is that [JsonProperty] Attribute isn't being respected, whenever I sent a request like this:

curl -X POST \
  -H "Content-Type: multipart/form-data" \
  -F "file=@/path/to/file.mp3" \
  -F "lang=en" \
  -F 'stamps=true' \
  -F "model=base" \
  -F "translate=true" \
  <insert url>


The TimeStamps doesn't get set to
true
even though it has the stamps attribute. If I set stamps in curl to timestamps it will work which leads me to believe that they're something wrong with the attributes. 🤔

I originally was using System.Text.Json but switch to Newtonsoft.Json thinking It might be a bug in System.Text.Json but sadly nothing changed.
image.png
Was this page helpful?