Sending a List of objects through PostAsync

leo del ciello 106 Reputation points
2024-10-28T08:06:40.0866667+00:00

I have a list of objects each one made like this :

public class PostElement {
public string field1 {get;set;}
public string field2 {get:set:}
public string field3 {get;set;}
}

I need to send this list :

List<PostElement> ElementList  = new List<PostElement>();

using a :

HttpClient.PostAsync (url , content); 


How should I create the content using the List above ?

I tried everything I could think of : serializing every element of the list to a JsonContent, and then inserting each of these JsonContent elements into a MultiPartContent ; serializing the whole list to a StringContent, serializing the whole list to a JsonContent.

None of these methods worked so far .

Is there any chance I could send this List using a PostAsync, or should I give up the idea ?

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | ASP.NET | Other
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,491 Reputation points
    2024-10-28T10:07:13.9+00:00

    We need basic information. Are you targeting .NET Framework ASP.NET Core? What is the client; Web Forms, MVC, Web API? What's the server; Web Forms, MVC, Web API? Can you at least provide the code you've tried both server and client?

    Is there any chance I could send this List using a PostAsync, or should I give up the idea ?

    Of course, the official documentation covers how to post a complex type.

    https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient


  2. SurferOnWww 5,026 Reputation points
    2024-10-28T12:18:48.9233333+00:00

    Do you want to serialize List<PostElement> object to JSON string and send it to web server using HttpClient?

    Yes Exaclty .I need to serialize the plain List into a Json string and send it by using a PostAsync

    Please see the following sample code:

    using System.Collections.Generic;
    using System.IO;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleAppSystemTextJson
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                // Create List<PostElement> object
                // for example:
                var elements = new List<PostElement>
                {
                    new PostElement { field1 = "a", field2 = "b", field3 = "c" },
                    new PostElement { field1 = "d", field2 = "e", field3 = "f" },
                    new PostElement { field1 = "g", field2 = "h", field3 = "i" }
                };
    
                // Serialize List<PostElement> object to JSON string
                var jsonString = System.Text.Json.JsonSerializer.Serialize(elements);
    
                // url to send JSON string
                var url = "https://example.com/api";
    
                // Create HttpRequestMessage
                var request = new HttpRequestMessage(HttpMethod.Post, url);
                request.Content = new StringContent(jsonString,
                                                    Encoding.UTF8,
                                                    "application/json");
    
                // Create HttpClient
                var client = new HttpClient();
    
                // Post the JSON string to the url
                var response = await client.SendAsync(request);
    
                // if you need to do something with response data
                using (Stream responseStream = await response.Content.ReadAsStreamAsync())
                {
                    // Proccess response data as required
                }
            }
        }
    
        public class PostElement
        {
            public string field1 { get; set; }
            public string field2 { get; set; }
            public string field3 { get; set; }
        }
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.