0

I'm trying to send large JSON objects to API via POST method. I heard about pagination and chunking methods. But I'm not sure how to implement them in c#(Windows console application).

Thank you for taking time and helping me out here.

  • Did you see [Uploading large files to Controller in chunks using HttpClient, IFormFile always empty](https://stackoverflow.com/q/53123243/3744182)? – dbc Apr 14 '20 at 16:27
  • Thank you very much for posting that. Can you please tell me how to implement them for JSON object instead of files? – murali Krishna Apr 14 '20 at 16:54
  • Well [this answer](https://stackoverflow.com/a/53159296/3744182) shows how to do it with a `Stream`, so try serializing your data model to a `MemoryStream` rather than a `string` for upload. See [here](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm#JsonSerializer) if you don't know how. You will want to leave the stream open after writing so set [`CloseOutput = false`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonWriter_CloseOutput.htm) and see [Is there any way to close a StreamWriter without closing its BaseStream?](https://stackoverflow.com/q/2666888). – dbc Apr 14 '20 at 17:02
  • FYI, I haven't tried that answer myself so I can't guarantee it will work. But it's accepted and upvoted so it looks to be worth a try. – dbc Apr 14 '20 at 17:03
  • Thanks for the help. I'll give it a try and let you know how it goes. I'm new to c#. – murali Krishna Apr 14 '20 at 17:11
  • (Actually you don't need to set `CloseOutput = false` as long as the `StreamWriter` doesn't close the underlying `Stream`.) – dbc Apr 14 '20 at 17:14
  • Firstly I tried to make use of that code. So I pasted my JSON in a file and passing it as parameter to it. I'm getting the following error. {StatusCode: 413, ReasonPhrase: 'Request Entity Too Large', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: close Date: Tue, 14 Apr 2020 18:19:19 GMT Server: nginx Content-Length: 192 Content-Type: text/html }} Code images https://imgur.com/U4A4tcv https://imgur.com/jcOAR9N – murali Krishna Apr 14 '20 at 18:22
  • Well it sounds like the server is refusing to allow such a large upload even when chunked. Maybe see [(413) Request Entity Too Large | uploadReadAheadSize](https://stackoverflow.com/q/10122957/3744182) or [RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient](https://stackoverflow.com/q/26710925/3744182) or [(413) Request Entity Too Large](https://stackoverflow.com/q/26723894/3744182). – dbc Apr 14 '20 at 18:24
  • yeah. I think the file is too large. Just now tried after decreasing the file size. That is a mock API. I should implement a API of my own and test it out. Thanks for the help. Can you share your email id to text more on this? – murali Krishna Apr 14 '20 at 18:28

0 Answers0