0

My .net 4.0 class library send HttpRequestMessage and receive HttpResponseMessage from .asp Web API (REST).

When I sent a small class, I use JSON to parse it as string, then I send string by:

request = new HttpRequestMessage();
request.RequestUri = new Uri(myRestAPI);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Method = method;
if (method != HttpMethod.Get)
   request.Content = new StringContent(content, Encoding.UTF8, mthv);

Next, use HttpClient to send it:

using (HttpResponseMessage httpResponse = _client.SendAsync(request).Result)
{..}

This works fine.
Now, my class has got bigger, How can i send it ?
What i did was to zip it and send as ByteArrayContent.

request = new HttpRequestMessage();
request.RequestUri = new Uri(url);
request.Method = method;
if (method != HttpMethod.Get)
   request.Content = new ByteArrayContent(content);
   request.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");

And send it the same way.

But now the server does reply me with error:

No MediaTypeFormatter is available to read an object of type 'Byte[]' from content with media type 'multipart/form-data'.

What am I doing wrong ?? I am trying to find a proper guide and all the guides are talking about uploading FROM web api and not about uploading from application to web api..

1
  • ByteArrayContent sends the media type application/octet-stream. If you really wanted to send multipart form data then you would use the MultiPartFormDataContent class. Either way, you shouldn't be changing the content-type just because you want to compress. That's why there is a separate content-encoding header. Commented Sep 11, 2013 at 14:01

1 Answer 1

3

Go get WebAPIContrib, it has a CompressedContent class

With it you can do,

request.Content = new CompressedContent(new StringContent(content, Encoding.UTF8, mthv),"gzip");

and compression will just magically just happen.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, is this valid for .net 4.0 ? When i receive it on my Controller, Do I do the same as before ? - before i just had the object as input parameter in (..[FromBody] mySerializedClass..)
@ilansch That class works fine in .net 4. As far as decompressing on the server. I think IIS will do it automatically, however, if you are on self host you might need a MessageHandler.
@ilansch The GZipStream class can decompress as well as compress. There is an example of how to do it here github.com/WebApiContrib/WebAPIContrib/blob/master/src/…
I was trying what you said, i receive null in my controller: [HttpPut] public HttpResponseMessage UpdateStatus([FromBody]MyClass systemBase, [FromUri]string systemId, [FromUri]string af = "") {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.