Alejandro
Alejandro

Reputation: 308

C# HttpClient post content with FormUrlEncodedContent object in Dictionary string/object

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; }
}

Upvotes: 3

Views: 9463

Answers (1)

Somnath Ghosh
Somnath Ghosh

Reputation: 138

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

Upvotes: 5

Related Questions