7

I am trying to talk to a REST service and I'm trying to call a POST method where I need to provide some data in the post body.

I have my model class all nicely set up something like this:

public class MyRequestClass
{
    public string ResellerId { get; set; }
    public string TransactionId { get; set; }
    ... other properties of no interest here ... 
}

and I'm using RestSharp in C# to call my REST service something like this:

RestClient _client = new RestClient(someUrl);

var restRequest = new RestRequest("/post-endpoint", Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("Content-Type", "application/json");

restRequest.AddJsonBody(request);   // of type "MyRequestClass"

IRestResponse<MyResponse> response = _client.Execute<MyResponse>(restRequest);

Everything seemed to work fine - no exceptions are thrown. But the service responds with:

We are experiencing problem in processing your request

When I looked at the request JSON that's being sent, I see that all the properties are in Capitalized spelling:

{ "ResellerId":"123","TransactionId":"456" }

and this is causing the issue - the service excepts them in all lowercase:

{ "resellerId":"123","transactionId":"456" }

So I tried to decorate my C# model class with attributes:

public class MyRequestClass
{
    [RestSharp.Serializers.SerializeAs(Name = "resellerId")]
    public string ResellerId { get; set; }

    [RestSharp.Serializers.SerializeAs(Name = "transactionId")]
    public string TransactionId { get; set; }
    ... other properties of no interest here ... 
}

but that didn't seem to change anything - the JSON request sill has the property names in a Capitalized spelling and thus the call fails.

How can I tell RestSharp to always use lowercase property names in the JSON generated from a C# model class?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • @CodeCaster: yes, seems to be dealing with the same problem. However: if move your answer there, I won't be able to accept it :-( – marc_s Feb 06 '15 at 11:42

1 Answers1

2

Edit: this answer is outdated. Read the thread shared by @marc_s. I wont remove this answer because it used to be helpful.

You can or you should add Json.NET to RestSharp.

There is a issue about this on the github repo of RestSharp.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • Now, that RestSharp got rid of the Json.NET dependeny (as per Version 103 (https://github.com/restsharp/RestSharp/blob/master/readme.txt)), is this sill the way to go today? – Marcel Aug 09 '19 at 14:18
  • @Marcel I don't know. Consider my answer as outdated – aloisdg Aug 11 '19 at 12:36