Api POST from code behind
api, asp.net, c#, post
Solution
POST using Form:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("Key1", "Value1"));
postData.Add(new KeyValuePair<string, string>("Key2 ", "Value2"));
HttpContent content = new FormUrlEncodedContent(postData);
var response = client.PostAsync("api/receipt/" + jID, content)
if (response.IsSuccessStatusCode)
{}
POST using JSON, assume you have Dto class:
var client = new HttpClient();
var dto = new Dto {Pro1 = "abc"};
var reponse = client.PostAsJsonAsync("api/receipt/" + jID, dto).Result;
if (reponse.IsSuccessStatusCode)
{}
Problem
I want to post an object using an API call. I'm getting the data using the following code in my codebehind ``` HttpClient client = new HttpClient(); client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("api/receipt/" + jID).Result; if (response.IsSuccessStatusCode) {} ``` I would like to know there is any code equivalent to POST for this.