3

I am trying to post a contect to my server. This is how I have been doing it for the past and it was working until I had to use objects besides strings.

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authType, tokens);
    var postParams = new Dictionary<string, object>();

    postParams.Add("string", string);
    postParams.Add("int", string);
    postParams.Add("datetime", DateTime);
    postParams.Add("datetime", DateTime);
    postParams.Add("Match", Match);
    postParams.Add("TicketId", token);

    using (var postContent = new FormUrlEncodedContent(postParams.ToDictionary()))
    {
        var myContent = JsonConvert.SerializeObject(postParams);
        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        using (HttpResponseMessage response = await client.PostAsync(@"http://url/api", byteContent))
        {
            response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
            using (HttpContent content = response.Content)
            {
                string result = await content.ReadAsStringAsync();
                var Json = JsonConvert.DeserializeObject<bool>(result);
                return Json;
            }
        }
    }
}

And this is how my request is supposed to be.

methode: POST
object: {
    "title":"test-ticket-2",
    "detail": "Description test create ticket in prod",
    "dateStart": "2019-10-06",
    "dateEnd": "2019-10-12",
    "ratio": "2.15",
    "matchResult": "2",
    "matchs": [
            {
                "Teams": "Test-match-1",
                "Proposal": "3x",
                "DateStart": "2019-10-06 18:00",
                "DateEnd": "2019-10-06 20:00",
                "Payout": "0.6"
            }
             ]

I have no idea IF and HOW I can add Objects other than string and make the request. Any ideas?

Edit: Match looks like this

public class Match
{
    public int Id { get; set; }
    public string Teams { get; set; }
    public string MatchResults { get; set; }
    public string Proposal { get; set; }
    public string Payout { get; set; }
    public DateTime? DateStart { get; set; }
    public DateTime? DateEnd { get; set; }
    public Uri Ball { get; set; }
    public int TicketId { get; set; }
}
haldo
  • 14,512
  • 5
  • 46
  • 52
Alejandro
  • 308
  • 2
  • 11
  • Are you trying to add a complex object to the Match class and you are wondering how that would go? – Rakesh Oct 21 '19 at 17:33
  • I am wondering how to to the request as FormUrlEncodedContent only accepts string/string – Alejandro Oct 21 '19 at 17:34
  • Go to http://quicktype.io and paste your JSON in there, have it generate some C# stubs for you – Caius Jard Oct 21 '19 at 17:37
  • 1
    You do `using (var postContent = new FormUrlEncodedContent(postParams.ToDictionary()))` -- but never actually use `postContent` for anything. Why is that? – dbc Oct 21 '19 at 17:47
  • Take a look at [How to post JSON with HttpClient using C#?](https://stackoverflow.com/q/28468484/3744182), [Send JSON via POST in C# and Receive the JSON returned?](https://stackoverflow.com/q/23585919/3744182), [How do I turn a C# object into a JSON string in .NET?](https://stackoverflow.com/a/19137100/3744182) and [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182). Do those 4 questions answer yours also? – dbc Oct 21 '19 at 17:51

1 Answers1

5

HOW I can add Objects other than string and make the request. Any ideas?

using (HttpClient httpclient = new HttpClient()) 
{
    Models.ApplicationUser applicationUser = new ApplicationUser();
    string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(applicationUser);
    StringContent stringContent = new StringContent(serialized);
    httpclient.PostAsync("url", stringContent);
}

Hope you want to do something like this

Alejandro
  • 308
  • 2
  • 11
Somnath Ghosh
  • 138
  • 1
  • 5
  • I guess yes but it is not one object. It is a mix of strings and User class. – Alejandro Oct 21 '19 at 19:41
  • @Alejandro - have you tried just adding a `postParams.Add("matchs", new List { /* Contents of the list */ });` and using your pre-existing code, bearing in mind that the `var postContent = new FormUrlEncodedContent(postParams.ToDictionary())` is never used and so could almost certainly be skipped? – dbc Oct 21 '19 at 20:31
  • @alejandro. Still it will work . Keep the string and class in another class – Somnath Ghosh Oct 22 '19 at 01:34
  • 2
    I needed different object in the dictionary. The solution was to declare both as objects and remove FormUrlEncodedContent as it was necessary. As your answer covers some part of it, it is accepted as an answer. – Alejandro Oct 24 '19 at 14:50