Nested objects are posted as NULL in post request

673 views
Skip to first unread message

REHMAN ALI

unread,
Oct 29, 2021, 7:37:31 PM10/29/21
to RestSharp
I want to Post some data to web API using rest api. Json schema is:
       "contact":
        { 
                       "id":123, 
                      "first_name":"Jimmy", 
                       "last_name":"Dee", 
                        "account_id":123, 
                        "owned_by":123, 
                        "email_addresses":[ 
                        { 
                                  "id":123, 
                                  "address":"ji...@jimmydees.com
                         } ], 
                        "phone_numbers":[ 
                         { 
                                   "id":123, "number":"123-123-1234", "phone_number_type":"Main",                                              "extension":"ex 5" 
                          }]
           } 
}

My code is:
// Endpoint: POST /v1/contacts.json?contact={contact parameters} 

var client = new RestClient($"https://api.tripleseat.com/v1/contacts.json") 
Authenticator = OAuth1Authenticator.ForProtectedResource(consumerKey, consumerSecret, null, null) }; 
var contact = new
            {
                first_name = "Foo",
                last_name = "foo",
                account_id = 15998446,
                owned_by = 175177,
                email_addresses = new [] { new { address = "teste...@gmail.com" } },
                phone_numbers = new[] { new { number = "1234567887" } },
             };
var request = new RestRequest("?contact=", Method.POST); 
request.AddJsonBody(contact);
RestResponse response = (RestResponse)client.Execute(request);

When I run this code the contact is created but have nested array as Null, response content with status code of 200 is:
      "contact":
       { 
              "id":21152554, 
              "first_name":"Foo", 
              "last_name":"foo", 
              "account_id":15998446, 
              "owned_by":175177, 
              "email_addresses":[ 
               { 
                     "id":21692948, 
                      "address":"" 
                }], 
               "phone_numbers":[ 
                 { 
                         "id":39428416, 
                          "number":null, 
                    } ] 
          } 
}

Problem is the nested arrays email_addresses and phone_number have null values in address and number. Kindly help me where I'm making mistakes.

Alexey Zimarev

unread,
Oct 31, 2021, 5:01:30 AM10/31/21
to RestSharp
It's better to use a proper serialiser for complex objects: https://restsharp.dev/usage/serialization.html#system-text-json

REHMAN ALI

unread,
Oct 31, 2021, 5:11:18 AM10/31/21
to RestSharp
Thank you for your reply but I have found the solution. the problem was in my anonymous type object and request Resource.
I solved this by changing:

var client = new RestClient($"https://api.tripleseat.com") 
Authenticator = OAuth1Authenticator.ForProtectedResource(consumerKey, consumerSecret, null, null) }; 
var newConatct = new
           contact=new
             {
                first_name = "Foo",
                last_name = "foo",
                account_id = 15998446,
                owned_by = 175177,
                email_addresses = new [] { new { address = "teste...@gmail.com" } },
                phone_numbers = new[] { new { number = "1234567887" } },
             };
var request = new RestRequest("Method.POST); 
request.Resource=String.Format("/v1/contacts.json");
request.AddJsonBody( newConatct );
RestResponse response = (RestResponse)client.Execute(request);

Reply all
Reply to author
Forward
0 new messages