ilansch
ilansch

Reputation: 4878

REST API Put large data

I have a REST API implemented in microsoft Web API.
In my client i use HttpRequestMessage and HttpResponseMessage.
When i am sending a small class, I serialize it to JSON and then send it.
Soon, my class becomes bigger, and I need to JSON the class, zip it (in memory) and send to server. I can no longer use the same technique, I need to send the zip in chunks.

What is the proper way to achieve it ? I have read this post Posting a File and Associated Data to a RESTful WebService preferably as JSON

Need some good articles, I dont know where to start.

Upvotes: 5

Views: 8818

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142044

On the client side this should work pretty much out of the box...

        var httpClient = new HttpClient();

        httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;

        var content = new CompressedContent(new StreamContent(new FileStream("c:\\big-json-file.json",FileMode.Open)),"UTF8");

        var response = httpClient.PostAsync("http://example.org/", content).Result;

You can find an implementation of CompressedContent in WebApiContrib. If you are using earlier than .net 4.5 the request will be buffered client side before sending. Unfortunately the underlying HttpWebRequest doesn't support buffered streaming until .net 4.5

Upvotes: 5

Related Questions