1

I am consuming an API endpoint on which I don't have any control.

API expects JSON post in this format

{
  "type": "test" 
}

I have a model

public class MyClass 
{

    public string type { get; set; }
}

Which I populate like this.

  MyClass myclass = new MyClass()
            {
                type = "something",
  }

because "type" is a reserve world in C# it changes the name to @type = "something",

When I call it using

HttpResponseMessage response = await client.PostAsJsonAsync("/abcd", myclass);

It sends the JSON

            {
                @type = "something",
  }

because API expect "type" and it gets @type, it throws an error bad request.

What is the best way I can pass "type" without @ sign in it?

---------------------- UPDATE ------------------------------

I got it sorted, It was replacing type to @type in the debugger (JetBrains rider) which was kind of misleading. that was only one part. the type was also enum in my code and It was showing the enum string in the debugger but during the serializing process, it changes it to a number (index) :( I used this,

var myContent = JsonConvert.SerializeObject(myclass);

just above my PostAsJsonAsync to see what it converts to, and it shows me the correct picture, type is type, but the value of type is number (index of the enum), changed it to string and all sorted. Thanks, guys. Lesson learnt, don't just trust the third party debugger blindly.

Developer
  • 25,073
  • 20
  • 81
  • 128
  • 1
    ? Afaik `type` is not a reserved word in [C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/), nor in [javascript](https://www.programiz.com/javascript/keywords-identifiers). `typeof` is one. And `Type` is a class in c#. – JHBonarius Mar 24 '22 at 07:56
  • Just for clearity: Are you using NewtonSoft.Json or System.Text.Json? Answers may be different for each. – Fildor Mar 24 '22 at 08:16
  • 1
    Are you using some custom or 3rd Party code analyzer, which doesn't like "type" as property name? It doesn't have a problem in my vanilla VS 2022. – Fildor Mar 24 '22 at 08:18
  • Using Jetbrain Ride library which shows type as @type in debugging mode. – Developer Mar 25 '22 at 02:33

1 Answers1

1

1 :

Add JsonProperty to @type

public class MyClass 
{
    [JsonProperty("type")]
    public string @type{ get; set; }
}

And serialize it using Newtonsoft.Json

MyClass myclass = new MyClass()
{
     @type = "something",
}
var value = JsonConvert.SerializeObject(myclass , Formatting.Indented);
HttpResponseMessage response = await client.PostAsJsonAsync("/abcd", value);

2 :

Add JsonPropertyName to @type

public class MyClass 
{
    [JsonPropertyName("type")]
    public string @type { get; set; }
}

And serialize it using System.Text.Json.JsonSerializer

var myclass = new MyClass()
{
   @type = "something",
}

string value = System.Text.Json
  .JsonSerializer.Serialize(myclass);

HttpResponseMessage response = await client.PostAsJsonAsync("/abcd", value);
spaleet
  • 838
  • 2
  • 10
  • 23
  • 1
    In "modern" c#, using `System.Text.Json`, its [JsonPropertyName](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpropertynameattribute). [ref](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties). – JHBonarius Mar 24 '22 at 08:22
  • thanks for you suggestion, I updated my answer and included both System.Text.Json and Newtonsoft.Json – spaleet Mar 24 '22 at 08:32
  • Thanks @Ali I used this to do debugging and updated my question as well. var myContent = JsonConvert.SerializeObject(myclass); – Developer Mar 25 '22 at 02:46