Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending List in http request in C#

I’m trying to send List (of strings) as http request to a flask python backend but I cant figure how to parse the list into json format to send that

I tryed this:

var myObject = (dynamic)new JsonObject();
myObject.List = new List<string>();
// add items to your list

according to this: C# HTTP post , how to post with List<XX> parameter?
but It’s says that List isn’t recognized.
any help or other method to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

please prefer this Microsoft documentation.

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to?pivots=dotnet-7-0

using System.Text.Json;

namespace SerializeToFile
{
public class WeatherForecast
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
}

public class Program
{
    public static void Main()
    {
        var weatherForecast = new WeatherForecast
        {
            Date = DateTime.Parse("2019-08-01"),
            TemperatureCelsius = 25,
            Summary = "Hot"
        };

        string fileName = "WeatherForecast.json"; 
        string jsonString = JsonSerializer.Serialize(weatherForecast);
        File.WriteAllText(fileName, jsonString);

        Console.WriteLine(File.ReadAllText(fileName));
    }
}
}
// output:
//{"Date":"2019-08-01T00:00:00- 
07:00","TemperatureCelsius":25,"Summary":"Hot"}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading