0

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);
    }
Jashvita
  • 553
  • 3
  • 24

1 Answers1

1

I am sending a post request to API, when the string value get large around 20000 characters, I get 404.15 error.

You are getting the expected error which means 404.15 Query String Too Long because we cannot send large data using querystring once it exeed the limit your data would be lost and encounter the error you are getting.

To resolve this issue I searched the net and found multiple resolutions but I did not get it right.

The element specifies limits on HTTP requests that are processed by the Web server. These limits include the maximum size of a request, the maximum URL length, and the maximum length for a query string. As you can see below:

enter image description here

Solution:

You may get many work around online but if you wouldn't point the root cause it may persist in longer operation. Though you can configure request limits in IIS. But the exact solution would be by using [FromBody] along with your class to submit your large data and it would work accordingly.

Note: If you would like to know more details on it you could check our official document here

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43