1

I was trying to POST base64 string of 2 image (4MB + in Size) in a object. Here is the Model Sample :

public class DemoModel
{
    public string No{ get; set; }
    public string OneBase64 { get; set; }
    public string TwoBase64 { get; set; }
}

Here is how I am trying to capture POST-ed data in webapi controller.

  [HttpPost]
  public string PostSimpleData([FromBody] DemoModel data)
    { 
       // do something 
    }

but when I post the json it shows serialization / deserialization error.

enter image description here

Then I added few lines in webconfig to increase response size & json size.

<system.web.extensions>
    <scripting>  
         <webServices>                                                   
             <jsonSerialization maxJsonLength="2147483647" />                 
         </webServices>
    </scripting>
</system.web.extensions>

and

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="2147483647" />
  </requestFiltering>
</security>

and

<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" />

Still no result. If I post anything except Base64 data It automatically serialize and bind with object in controllers method.

here is the JSON I am trying to POST.

{
   "NO" : "01",
   "OneBase64" : "Some base 64 Data",
   "TwoBase64" : "Some base 64 Data"
}

any kind of help is appreciated.

2 Answers2

1

I changed my method from this

[HttpPost]
public string PostSimpleData([FromBody] DemoModel data)
{ 
   // do something 
}

to this

 [HttpPost]
 public string PostSimpleData()
 {
        Stream req = Request.InputStream;
        req.Seek(0, System.IO.SeekOrigin.Begin);
        string json = new StreamReader(req).ReadToEnd();
        DemoModel model = JsonConvert.DeserializeObject<DemoModel>(json);

   //Now i Can Do whatever I want with this data
 }

and it worked like a charm. Thanks to this post here ASP.Net WEB API Failing POST Large JSON data from Mobile

-1

You need to configure not your web api but your sender serializer instance directly by applying serializer.MaxJsonLength = Int32.MaxValue; from where are you sending that json object

svyat1s
  • 868
  • 9
  • 12
  • 21
  • I am sending data from android & web (vue js ) application. in android I made a json using GSON . Gson gson = new Gson(); String json = gson.toJson(data); after this I post the data like this. String response = mReq.sendPost(fullUrl, json, "application/json"); can u suggest where should I make the change ????? – Khaled Md Tuhidul Hossain Oct 02 '20 at 17:53
  • 1
    No, this answer does not apply at all. The error is happening on the server when trying to convert the JSON into a .NET object, so changing the settings for the serializer on the client would have no affect. – mason Oct 02 '20 at 17:55