0

Using RestSharp, I need to POST a body containing a json string that looks like this:

{
    "$a": "b",
    "c": "d"
}

In the past I've created RestSharp requests using code like this:

var request = new RestRequest("someApiEndPoint", RestSharp.Method.POST);
request.AddJsonBody(new
{
    a = "b",
    c = "d"
});

What's the best way to add a "$" to the "a" property in this case?

newbyca
  • 1,523
  • 2
  • 13
  • 25

1 Answers1

2

Since you are using an anonymous type, you could just as easily switch to using a dictionary:

var root = new Dictionary<string, object>
{
    {"$a", "b" },
    {"c", "d" },
};
var request = new RestRequest("someApiEndPoint", RestSharp.Method.POST)
    .AddJsonBody(root);

If you were using an explicit type, you could check RestSharp serialization to JSON, object is not using SerializeAs attribute as expected for options.

dbc
  • 104,963
  • 20
  • 228
  • 340