I'm using RestSharp 106.11.7.
I'm trying to create a task on ADO. I'm using RestSharp as follows:
var body = new AdoRequestBody
{
op = "add",
path = "/fields/System.Title",
from = null,
value = "Sample task"
};
var request = new RestRequest("aStringPointingtoApi", Method.POST);
request.AddJsonBody(body);
With this, the body is being serialized to:
{
"op": "add",
"path": "/fields/System.Title",
"from": null,
"value": "Sample task"
}
According to ADO API this is the correct body:
[
{
"op": "add",
"path": "/fields/System.Title",
"from": null,
"value": "Sample task"
}
]
I tried the body with array/list parenthesis in Postman and it's working. If I remove the array/list parenthesis it doesn't work.
This is the error message I get:
You must pass a valid patch document in the body of the request.
Someone here seems to have found a solution using Newtonsoft.Json.JsonConvert, which I don't want to use. https://github.com/restsharp/RestSharp/issues/1413#issuecomment-578302527
So, how do I get RestSharp to serialize my object inside an array parenthesis?
CodePudding user response:
The API expects an array of AdoRequestBody
objects and you are serializing a single object. As stated in comments, create an array with your single object, then serialize it for the request:
var body =
new AdoRequestBody[]
{
new AdoRequestBody
{
op = "add",
path = "/fields/System.Title",
from = null,
value = "Sample task"
}
};
var request =
new RestRequest(
"aStringPointingtoApi",
Method.POST);
request.AddJsonBody(body);