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.