Eli

Eli

  • 0
  • 6
  • 0

Failing to make a patch via API using c# and json

Oct 13 2017 2:44 AM
I am using C# to call an API to make an update on a record. there is a class for this patch operation which has Op, Path, and Value defined as strings. However, the value needs to be passed as an array which has several items inside it. I can make this work via Postman, but cannot make it work in C#. This is my code below:
 
CustomerValue mycustomer = new CustomerValue();
{
mycustomer.Id = 5;
mycustomer.Caption = "Customer Testing";
mycustomer.Value = "Test value";
}
List<CustomerValue> cf = new List<CustomerValue>
{
mycustomer
};
 
string jsonstr = JsonConvert.SerializeObject(cf);
var client = getApiClient();
var custApi = new CustApi(client);
var patchOps = new List<PatchOp>
{
new PatchOp
{
Op = "replace",
Path = "/customFields",
Value = jsonstr
}
};
var pResponse = custApi.UpdateCustomer(custid, patchOps);
When I look at patchOps, I see a similar string to the json string I get from Postman except for one additional quote that wrap jsonstr on each side.
 
Example of Postman string:
[{\"op\":\"replace\",\"path\":\"/custFields\",\"value\":[{\"id\":5,\"caption\":\"Cust Rating\",\"type\":0,\"entryMethod\":1,\"numberOfDecimals\":0,\"value\":\"Red - Bad\"}]}]"
 
mine will have an additional \" before and after of each of the internal brackets.
 
Any help please?