Mohan Chandra

Mohan Chandra

  • NA
  • 101
  • 16.6k

How to make a post method and call it from my desktop applic

Jun 9 2017 6:06 AM
hi,
I need to implement syncing accounting items to cloud,so I have already WCF methods to sync but I want web API to post json data and then it store in web DB.

like earlier we made a get method for android application and return data in json format.
 
   [AcceptVerbs("Get")]       
   public DataTable Inflow(long TenantId, long SalesPersonId)    
     {             
      DataTable dtReceivable = new DataTable(); return dtReceivable
     }
 for this android developer was calling this url like

MyURL/api/MobileAPI/Inflow?TenantId=1234&SalesPersonId=22
and then getting data in json format

Now I want to create a Post Method for syncing with web DB.
string JsonString = Inventory();
I need to pass this json string with TenantId.
I want to post this via my URL like
using (HttpClient httpclient = new HttpClient())
{
string url = "http://localhost:51411/api/MobileAPI/Inventory?TenantId=" + CommonClass.tenantId + "";
String uriToCall = String.Format(url);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", str)
});
}
 
I am not getting how to start so please can anyone can help me.
 
I tried this by reading some google articles but still its not fine....
 
string str = Inventory();
//HttpClient http = new HttpClient();
using (HttpClient httpclient = new HttpClient())
{

var http = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:51411/api/MobileAPI/Inventory?TenantId='" + CommonClass.tenantId + "'&JsonInventory='" + str));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
}
 
 and my web API code is
 
[HttpPost]
[AcceptVerbs("POST")]
public HttpResponseMessage Inventory(long TanantId, [FromBody]string JsonInventory)
{
try
{
List<Inventory> lstInventory = JsonConvert.DeserializeObject<List<Inventory>>(JsonInventory);
foreach (var item in lstInventory)
{
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (Exception ex)
{
throw ex;
}
}
 
My Inventory Model is this
 
public class Inventory     
{        
 public string ItemId { get; set; }  
       public string Category { get; set; }   
      public decimal Price1 { get; set; }    
     public string Description { get; set; }  
       public int QuantityAvailable { get; set; }  
       public int QuantityOnHand { get; set; }     
    public string Method { get; set; }    
 }
 
 I want to call my web API Inventory Method via url and send collection of json string data.
please help me how to call url then during calling my break point hit 
 
 
 

Answers (1)