title: Webapi custom JsonMediaTypeFormatter
body: <p>I am trying to create a custom JSONMediaTypeFormatter in which posts some json parameters to a webapi call.  I need to encrypt the returned data from the webapi, hence writing a custom mediatypeformatter.</p>
  
  <p>In my webapiconfig I clear all formatters and only add the custom formatter.</p>
  
  <pre><code>        config.Formatters.Clear();
          config.Formatters.Add(new CipherMediaFormatter());
  </code></pre>
  
  <p>In my custom media type formatter I add in the relevant headers and types but i still cant call my web api.  It gives me an error</p>
  
  <pre><code>     No MediaTypeFormatter is available to read an object of type 'Param' from content with media type        'application/json
  </code></pre>
  
  <p>The code for the mediatypeformatter</p>
  
  <pre><code>public class CipherMediaFormatter : JsonMediaTypeFormatter
  {
      private static Type _supportedType = typeof(object);
  
      public CipherMediaFormatter()
      {
          this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
          this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
          this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
      }
  
      public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
      {
          base.SetDefaultContentHeaders(type, headers, mediaType);
          headers.ContentType = new MediaTypeHeaderValue("application/json");
      }
  
      public override bool CanReadType(Type type)
      {
          return true;
      }
  
      public override bool CanWriteType(Type type)
      {
          return true;
      }
  
      public override Task&lt;object&gt; ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
      {
          var taskSource = new TaskCompletionSource&lt;object&gt;();
          try
          {
              var ms = new MemoryStream();
              readStream.CopyTo(ms);
              taskSource.SetResult(ms.ToArray());
          }
          catch (Exception e)
          {
              taskSource.SetException(e);
          }
          return taskSource.Task;
      }
  
      public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
      {
          var taskSource = new TaskCompletionSource&lt;object&gt;();
          try
          {
              if (value != null)
              {
                  var jsonData = JsonConvert.SerializeObject(value);
                  ICryptographicService cgService = new CryptographicService();
                  string apiKey = string.Empty;
  
                  string pattern = @"api\/(.*)?\/(\d+)?";
                  var match = Regex.Match(HttpContext.Current.Request.RawUrl, pattern);
                 ....
              }
          }
          catch (Exception e)
          {
              taskSource.SetException(e);
          }
          return taskSource.Task;
      }
  }
  </code></pre>