I am sending a post request to API, when the string value get large around 20000 characters, I get 404.15 error. If I set string to few characters then post request is successful. To resolve this issue I searched the net and found multiple resolutions but I did not get it right. Specialy the following link Send HTTP POST request in .NET.
So I am adding my code below and hoping someone can help me get this resolved
API Code
[Route("InsertData")]
[AcceptVerbs("GET", "POST", "PUT")]
public IActionResult InsertData(string Char_2000,string Char_1000)
{
try
{
returnAuthNumber = _myRepository.InsertData(Char_2000,
Char_1000,
if (returnAuthNumber == null) throw new NullReferenceException("Data could not loaded");
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
_logger.LogError(ex.StackTrace);
return Problem(statusCode: 500,
detail: ex.Message);
}
return Ok(returnAuthNumber);
}
Client request to the api. I don't know how logically correct it is but it is working when string char are just few characters.
public bool Post<T>(Controller mvcContrllr, string postURLString, string getURLString, string authToken, Dictionary<string, string> param = null)
{
var token = authToken;
var url = System.Configuration.ConfigurationManager.AppSettings["ApiUrl"] + this.BuildCompleteUri(postURLString, param);
var request = new HttpRequestMessage(HttpMethod.Post, this.BuildCompleteUri(url, param));
var client = new HttpClient();
// client.BaseAddress = new Uri(GlobalVariables.apiUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
return true;
}
return false;
}
Update I have fixed the problem by following the advice from the members reply. Just adding the code so it can help others. Basically, from client side I modified the post method to send complex type (class) instead of simple types in list of parameters. On API, I modified the method to accept complex type instead of simple type. Note that by default API method accepts simple types from query string and complex type from body [FromBody].
client side request
public List<T> Post<T>(Controller mvcContrllr, string postURLString, string getURLString, string authToken, MyClass myclass, Dictionary<string, string> param = null)
{
var token = authToken;
var url = System.Configuration.ConfigurationManager.AppSettings["ApiUrl"] + postURLString;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var urlParm = url;
HttpResponseMessage response = client.PostAsJsonAsync(urlParm, myClass).Result;
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync();
content.Wait();
var items = JsonConvert.DeserializeObject<List<T>>(content.Result);
return items;
}
return null;
}
API
[Route("InsertTAData")]
[AcceptVerbs("GET", "POST", "PUT")]
public IActionResult InsertData([FromBody] Myclass myClass)
{
try
{
returnAuthNumber = _myRepository.InsertData(myClass);
if (returnAuthNumber == null) throw new NullReferenceException("Data could not loaded");
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
_logger.LogError(ex.StackTrace);
return Problem(statusCode: 500,
detail: ex.Message);
}
return Ok(returnAuthNumber);
}