Automatically add Content-Type to incoming HTTP requests if missing


I'm developing an ASP.Net WebAPI service to allow embedded devices to transfer data in semi-realtime.

These devices will be on a capped wireless broadband plan, which means that low data consumption usage is a requirement for this project.

The devices will send data through HTTP in JSON format. At the moment, I include "Content-Type: application/json" in the header, which works well.

However, I'm thinking it is a waste of precious bytes (and data usage plan) to send these few bytes over the wire everytime. I can be 100% sure, at the moment that the data sent will be encoded by JSON, so I would rather have the server assume the Content-Type when it is missing.

However, if I omit it, my Post controllers do no trigger anymore when the client posts to them. They are declared like that:

public HttpResponseMessage Post([FromBody] MyEvent myEvent)

In short, what I'm trying to do is to automatically add, on the server-side, the Content-Type to the incoming HTTP requests that the client would have omitted to add, or another way of making my controllers work and automatically deserialize the JSON message, even with that Content-Type header entry is missing.

I'm hoping there might be a source code file or configuration on the WebAPI project where I could configure that?

Thanks in advance.


Try this.

public class ConTypeFilter:DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if ( request.Content.Headers.ContentType==null) { request.Content.Headers.ContentType=new MediaTypeHeaderValue("application/json"); } return base.SendAsync(request, cancellationToken); } }

And in your Application_Start() add the following.

GlobalConfiguration.Configuration.MessageHandlers.Add(new ConTypeFilter());

Thanks to <a href="http://www.asp.net/web-api/overview/advanced/http-message-handlers" rel="nofollow">this article</a>


  • How to compare with a “reference date”, then fill missing data in R?
  • how to return json from method inside an AMD module?
  • Do you need to call init_timer() again after a del_timer()
  • OCR & OpenCV: Difference between two frames on high resolution images
  • Fortran function returning unallocated array causes segmentation fault
  • Spring integration warning Referenced bean 'org.springframework.scheduling.support.PeriodicTrig
  • Does ruby on rails have client side validation like ASP.NET MVC
  • Escape double quotes within double quotes
  • How to capture or listen to browser notifications?
  • LCD Programming with Arduino
  • openssl handshake failed
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • uniform generation of points on 3D box
  • SharedPreferences or SQLite Database?
  • Textfile Structure (tables)
  • copying resource to sdcard gives a damaged file in android
  • Spark fat jar to run multiple versions on YARN
  • Q promise. Difference between .when and .then
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • vba code to select only visible cells in specific column except heading
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Large data - storage and query
  • align graphs with different xlab
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • WOWZA + RTMP + HTML5 Playback?
  • Buffer size for converting unsigned long to string
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Binding checkboxes to object values in AngularJs