Search code examples
c#asp.netasp.net-web-apihttp-status-code-413

Web API returns status 413


I am consuming a ASP.Net WEB API written in c# hosted in IIS6. When making a POST to the API it returns HTTP status 413. The API (not WCF) returns response as long as the content in the body is around 32+KB. If the size is like 40 KB then it errors out.

Below is the code snippet on the consumer side

string apiUrl = "https://a.com/api/emails/send";
using (WebClient client = new WebClient())
{
    client.Headers.Add("Content-Type", "application/json");
    client.Headers.Add("Accept", "application/json");
    try
    {
        var jsonResponce = client.UploadString(apiUrl, jsonRequest);

        var sendEmailResponce = JsonConvert.DeserializeObject<SendEmailResponce>(jsonResponce);
        var  emailMessageId = sendEmailResponce.EmailMessageId;
        Console.WriteLine("email sent.");
    }
    catch (WebException exp)
    {
        var  error = exp.ToString();
        Console.WriteLine(error);
    }
    catch (Exception exp)
    {
        var error = exp.Message;
    }
}

I am using IIS6 . Is there any setting in IIS / Code changes on the client might help me to get around this issue?


Solution

  • try this,

    1. Launch “Internet Information Services (IIS) Manager”
    2. Select the site that you are hosting your web application under it.
    3. In the Features section, double click “Configuration Editor”
    4. Under “Section” select: system.webServer then serverRuntime
    5. Modify the “uploadReadAheadSize” section to be like 20MB (the value there is in Bytes)
    6. Click Apply.