post method in c# code example

Example 1: how to POST in c#

using System;
using System.Collections.Specialized;
using System.Net;

namespace API_Console_App
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare the API Endpoint URL
            string url = "URL_HERE";

            // Create a New WebClient
            WebClient client = new WebClient();

            // Create a New NameValueCollection
            NameValueCollection values = new NameValueCollection();

            // Add Data to the NameValueCollection
            values.Add("data-name", "content");

            // Upload the NameValueCollection Values to the URL
            client.UploadValues(url, values);
        }
    }
}

Example 2: how make a post request c#

//You can aquire RestSharp from Nuget package manager
  RestClient restClient = 
  new RestClient(string.Format("{0}/myservice/api/endpoint", "https://exampledomain.com:88"));
  RestRequest request = new RestRequest(Method.POST);
  request.RequestFormat = DataFormat.Json;
  request.AddJsonBody(data); //data is C# model for post request
  request.AddHeader("Accept", "application/pdf");
  var result = restClient.Execute(request);