skip to Main Content

this is the json body I send using postman and it works (API respond is Boolean so I get true in this case. )

{ "ClubID":"A9FEE6F-FBBB-EB11","ContactID":"B6F1-43A48402F","LoginDate":"2017-11-19T22:00:00+2"}

and that is the function am trying to use to make the post request and get the respond back.

     var http = (HttpWebRequest)WebRequest.Create(url);
            http.Accept = "application/json";
            http.ContentType = "application/json";
http.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +
                                  "Windows NT 5.2; .NET CLR 1.0.3705;)");
        http.Method = "POST";
        String json = Newtonsoft.Json.JsonConvert.SerializeObject(listOfMem);
        UTF8Encoding encoding = new UTF8Encoding();
        Byte[] bytes = encoding.GetBytes(json);

        Stream newStream = http.GetRequestStream();
        newStream.Write(bytes, 0, bytes.Length);
        newStream.Close();

        var response = await http.GetResponseAsync();

        var stream = response.GetResponseStream();
        var sr = new StreamReader(stream);
        var content = sr.ReadToEnd();
        string res = content.ToString();
        

2

Answers


  1. Since we using .net core, please drop off using of the acient HttpWebRequest, let he rest in peace…

    this template should work

    // A class that hold the request
    public class MyHttpRequestData
    {
        public string ClubID { get; set; }
        public string ContactID { get; set; }
        public DateTime LoginDate { get; set; }
    }
    
    // Request
    var myContent = new MyHttpRequestData(); // Initialize
    using var request = new HttpRequestMessage
    {
        Content = new StringContent(JsonSerializer.Serialize(myContent), Encoding.UTF8, "application/json"),
        Method = HttpMethod.Post,
        RequestUri = new Uri("Url goes here!")
    };
    using var httpClient = new HttpClient();
    using var response = await httpClient.SendAsync(request);
    // do whatever it is with the response
    
    Login or Signup to reply.
  2. To post JSON data to your API endpoint, you can also try the following code snippet.

    using System.Net.Http;
    using System.Net.Http.Json;
    

    using HttpClientJsonExtensions.PostAsJsonAsync method to make POST request with JSON data.

    var client = new HttpClient();
    
    var response = await client.PostAsJsonAsync(url, listOfMem);
    if (response.IsSuccessStatusCode)
    {
    
        //...
    
        var res = await response.Content.ReadFromJsonAsync<bool>();
    
        //...
    
    }
    

    API action for testing purpose

    [HttpPost]
    public IActionResult Create([FromBody]MyTestModel myTestModel)
    {
        //...
        //code logic
        //...
    
        return Ok(false);
    } 
    

    Test Result

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search