none
Basic HttpClient Post question/sample RRS feed

  • Question

  • Can someone provide a sample of how I would use HttpClient to post the following request:

    Posting the following info to https://www.somerequesturl.com:

    {
    "Tester":{
    "Id": "joe_tester",
    "Key": "1243567890"
    },
    "User":{
    "Id": "my_id",
    "Key": "304o5kgi40388"
    },
    "ReturnUrl": "https://www.testsite.com/return/",
    "CallbackUrl": "https://www.testsite.com/callback/"
    }


    • Edited by Freq3 Friday, July 6, 2018 4:50 PM
    Friday, July 6, 2018 4:46 PM

Answers

  • So the issue is with formatting the JSON data? In that case I would recommend that you use simple .NET types and then use a JSON serializer like JSON.NET. It is cleaner, simpler and pretty common.

    //Guessing at data meaning here
    public class UserReference
    {
       public string Id { get; set; }
       public string Key { get; set; }
    }
    
    
    public class MyData
    {
       public UserReference Tester { get; set; }
       public UserReference User { get; set; }
    
       public string ReturnUrl { get; set; }
       public string CallbacUrl { get; set; }
    }
    
    
    var data = new MyData() { … };
    
    //Example using JSON.NET
    var json = JsonConvert.SerializeObject(data);
    
    
    var content = new StringContent(json, Encoding.Utf8, "application/json");
    
    

    Note that your JSON seems to be using Pascal casing which is odd for JSON. You might need to adjust the settings of the serializer (see here) to get it to format the JSON the way you want). Things to consider include casing, whether to include null, objects, how enums are converted etc.

    If the response coming back is JSON then you can use DeserializeObject to convert back as well.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, July 7, 2018 9:04 PM
    Moderator

All replies

  • There are plenty of examples online on how to do this based upon the many different factors that you'd have to take into account including the request/response format, the URL vs resource, authentication, etc. Given your example it could be done this way (but this assumes your example is actually correct and not an oversimplification of the actual call you're trying to make).

    var url = "";
    var jsonData = "{}";
    var client = new HttpClient();
    client.DefaultRequestHeaders.Add("Content-Type", "application/json");            
    
    using (var response = client.PostAsync(url, new StringContent(jsonData, Encoding.UTF8, "application/json")).Result)
    {
    };
    Getting the response back is a completely different story depending upon the format being used. 


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, July 6, 2018 5:38 PM
    Moderator
  • I understand how to post key/value pairs("Id": "joe_tester") , what I'm not understanding is how to post nested key/value pairs ("Tester": {"Id: "Joe_Tester", "Key:" "asdasdf"})
    Saturday, July 7, 2018 8:35 PM
  • So the issue is with formatting the JSON data? In that case I would recommend that you use simple .NET types and then use a JSON serializer like JSON.NET. It is cleaner, simpler and pretty common.

    //Guessing at data meaning here
    public class UserReference
    {
       public string Id { get; set; }
       public string Key { get; set; }
    }
    
    
    public class MyData
    {
       public UserReference Tester { get; set; }
       public UserReference User { get; set; }
    
       public string ReturnUrl { get; set; }
       public string CallbacUrl { get; set; }
    }
    
    
    var data = new MyData() { … };
    
    //Example using JSON.NET
    var json = JsonConvert.SerializeObject(data);
    
    
    var content = new StringContent(json, Encoding.Utf8, "application/json");
    
    

    Note that your JSON seems to be using Pascal casing which is odd for JSON. You might need to adjust the settings of the serializer (see here) to get it to format the JSON the way you want). Things to consider include casing, whether to include null, objects, how enums are converted etc.

    If the response coming back is JSON then you can use DeserializeObject to convert back as well.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, July 7, 2018 9:04 PM
    Moderator
  • Hi Freq3,

    We need to serialize JSON object to string and post it to the service, Then the service could receive the message and deserialize the string to related object as CoolDadTx mentioned.

    https://code.msdn.microsoft.com/windowsapps/How-to-use-HttpClient-to-b9289836

    Best regards,

    Zhanglong


    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, July 9, 2018 2:52 AM
    Moderator