none
pass multiple objects to json request as a single object RRS feed

  • Question

  • hi,

        

    public static async Task<string> CallPostApi(string path, string _sessionId)
            {
                UserRequest uu = new UserRequest();
                Availability aa = new Availability();
                Name nn = new TestClient.Name();

                uu.extId = "4410184";
                aa.available = "Yes";
                nn.given = "vish";


                var client = new RestClient(path);
                var request = new RestRequest(Method.POST);
                request.AddHeader("Postman-Token", _sessionId);
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Authorization", "Bearer "+_sessionId);
                request.AddHeader("Content-Type", "application/json");

                var json = JsonConvert.SerializeObject(uu);   here i am passing only one object i want to pass "aa" and "nn" also please help me how to map "aa" and "nn" to "uu".

                request.AddParameter("undefined", json, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);         
                return response.ToString();
            }


    Regards, vishal


    • Edited by b vishal Friday, March 15, 2019 5:40 PM
    Friday, March 15, 2019 5:40 PM

All replies

  • That is completely up to the remote endpoint. You have little control over the layout of the data you send, just the format (JSON, XML, etc). You need to look at the API's documentation and they will tell you exactly how the data needs to be sent. If they support multi-part forms then you'll send it that way. If they require a JSON object then you'll have to put your data in the format they require. If they support "multiple objects" then they will tell you how it has to be specified. If they don't then you cannot call it that way.

    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Tim Roberts Friday, March 15, 2019 9:49 PM
    Friday, March 15, 2019 6:49 PM
    Moderator
  • If I can expand just a bit on Michael's correct answer, the JSON spec requires that each document be one thing: object, list or scalar.  If your server handles it, you can combine the three objects into a single List<object> and pass that to the serializer, but as Michael pointed out, you have to make sure the other end is prepared to receive data that way.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, March 15, 2019 9:49 PM
  • Hi,

    This is my code

    public class UserRequest
        {
            public string externalId { get; set; }
            public Availability availability { get; set; }
            public Name name { get; set; }

        }

        public class Availability 
        {
            public string available { get; set; }
        }

        public class Name 
        {
            public string family { get; set; }
            public string middle { get; set; }
        }

    public static async Task<string> CallPostApi(string path, string sessionId)
            {
                UserRequest uu = new UserRequest();
                Availability aa = new Availability();
                Name nn = new TestClient.Name();

                uu.externalId = "4410184";
                aa.available = "Yes";

                nn.family = "b";
                nn.middle = "v";      

                var client = new RestClient(path);
                var request = new RestRequest(Method.POST);
                request.AddHeader("Postman-Token", sessionId);
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Authorization", "Bearer "+sessionId);
                request.AddHeader("Content-Type", "application/json");

                var json = JsonConvert.SerializeObject(uu);
                request.AddParameter("undefined", json, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);         
                return response.ToString();
            }

    how to map Availability class data and Name class data to UserRequest class.


    Regards, vishal


    • Edited by b vishal Saturday, March 16, 2019 4:55 AM
    Saturday, March 16, 2019 4:54 AM
  • So the API that you're calling accepts a UserRequest object with the data structure that you specified? If not then none of this is going to work.

    var request = new UserRequest()
    {
       externalId = "4410184",
       availability = new Availability()
       {
          available = "Yes"
       },
       name = new Name()
       {
          family = "b",
          middle = "v"
       }
    };
    
    var json = JsonConvert.SerializeObject(request);

    Or if you prefer to build things up in a more traditional approach.

    var request = new UserRequest();
    request.externalId = "4410184";
    
    var availability = new Availability();
    availability.available = "Yes";
    request.availability = availability;
    
    var name = new Name();
    name.family = "b";
    name.middle = "v";
    request.name = name;
    
    var json = JsonConvert.SerializeObject(request);

    Object initializers are shorter to type but both approaches do the same thing.

    Note that if you get any error from the response then you're not sending the correct data in. I notice you're using Postman (because of the token, which you shouldn't send by the way). Most APIs provide a Postman collection with the request/response data provided as an example. You'll need that. If you don't have it then you'll need to contact the developers to get the example documentation.


    Michael Taylor http://www.michaeltaylorp3.net


    Saturday, March 16, 2019 5:49 AM
    Moderator
  • I don't think you are reading the replies you've already received.  **WE** can't tell you how to combine those, or even if it is possible.  YOU have to find out what your server is prepared to accept.  Will your server accept the combined structures?  Only you can learn that, because only you have access to the documentation.  Without that documentation, it is impossible to know.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, March 17, 2019 7:03 AM
  • hi,

    below code worked for me 

                TestClient1.UserRequest uu = new TestClient1.UserRequest();
                TestClient1.Availability aa = new TestClient1.Availability();
                TestClient1.Name nn = new TestClient1.Name();
                uu.name = nn;//mapping
                uu.availability=aa;//mapping


    Regards, vishal

    Sunday, March 17, 2019 12:11 PM
  • hi,

    below code worked for me 

                TestClient1.UserRequest uu = new TestClient1.UserRequest();
                TestClient1.Availability aa = new TestClient1.Availability();
                TestClient1.Name nn = new TestClient1.Name();
                uu.name = nn;//mapping
                uu.availability=aa;//mapping

    Regards, vishal

    Sunday, March 17, 2019 12:11 PM
  • hi,

    below code worked for me 

                TestClient1.UserRequest uu = new TestClient1.UserRequest();
                TestClient1.Availability aa = new TestClient1.Availability();
                TestClient1.Name nn = new TestClient1.Name();
                uu.name = nn;//mapping
                uu.availability=aa;//mapping

    Regards, vishal

    Sunday, March 17, 2019 12:11 PM
  • TestClient1 looks like either a static class you defined or a property on the class you are putting this code in. It is generally not recommended you use either approach. For a simple test app this may work but it won't work in code where you're dealing with multiple requests at once (i.e. web apps or any multi threaded code).

    Create a new UserRequest object into a local variable, set the properties and then pass that to the client. If the request is more complex to set up then accept it as a parameter instead. Requests parameters to an API call and should rarely be saved outside the context of the call itself. My original answer demonstrated the general approach. If you want to expand on that a little then UserRequest can pre-create the other 2 nested types as well but this just cuts down on a new statement in your calling code.


    Michael Taylor http://www.michaeltaylorp3.net

    Sunday, March 17, 2019 9:56 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,
    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 18, 2019 1:53 AM
    Moderator