Developing a custom JsonMediaTypeFormatter for a Webapi platform

I'm currently working on developing a custom JSONMediaTypeFormatter to send some JSON parameters to a web API endpoint. The goal is to encrypt the data returned from the web API, which requires creating a customized media type formatter.

Within my webapiconfig file, I remove all existing formatters and replace them with the custom formatter I've created:

config.Formatters.Clear();
config.Formatters.Add(new CipherMediaFormatter());

Although I have added relevant headers and types in my custom media type formatter, I am encountering an error when trying to call my web API:

No MediaTypeFormatter is available to read an object of type 'Param' from content with media type 'application/json'.

Here is the code snippet for the media type formatter:

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");
   }

   // Other methods...

}

Answer №1

The issue stemmed from the usage of ReadFromStreamAsync, which was causing an error with the mediatypeformatter.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Convert JSON string to a C# list for a dropdownlist

I am working on a Windows Form application where I need to deserialize a JSON string obtained from a web address in order to extract two specific values. Can someone guide me on how to achieve this? Here is the code snippet I have for fetching the JSON st ...

What is the reason behind the angular http client's inability to detect plain text responses?

When I make a call to httpClient.get, I notice in Fiddler that both the request and response headers have text/plain format. Request: Accept: application/json, text/plain, */* Response: Content-Type: text/plain; charset=utf-8 Even though I am returning a ...

retrieve JSON response from struts2 controller

My web application utilizes jquery and struts2. I am now facing the task of embedding a Google map into my webpage and adding some markers to it. To accomplish this, I have used the jquery.getJSON() command to send a request to a struts2 action. Here is a ...

No response from jQuery's $.getJSON() function

I'm currently experimenting with jQuery by using a script that I wrote. As a beginner in learning jQuery, I am trying to read data from a .json file and display it in a div. function jQuerytest() { $.getJSON( "books/testbook/pageIndex.json", func ...

Tips for extracting JSON data from an API with identical names as values

I am working on a project to create a data search system using JSON. The JSON data is stored in a REST API, and the structure of the API is as follows: [ { "info": "cute but big animal", "type": "pig", ...

What is the best way to save a jQuery or JavaScript variable into a JSON file?

Is there a way to save a jquery variable in a json file? I have the following: var image='/test/test.png'; I am obtaining this path through file upload: <input type="file" name="imageurl" value="imagefile"></input> Therefore, I ne ...

Discover the power of integrating JSON and HTTP Request in the Play Framework

My aim is to develop a methodology that can effectively manage JSON and HTTP requests. This approach will facilitate the transition to creating both a Webapp and a Mobile App in the future, as JSON handling is crucial for processing requests across differe ...

The policy document appears to be malformed while attempting to execute the CreateRole operation

Just starting out with python and boto3, so please bear with me if this is a simple oversight on my part. I'm attempting to create a role in my Lambda function using python and boto3. Within my Lambda function, I have two files: roles.py and roles.co ...

JSON is not conforming to the standard nesting level

After simplifying, the JSON implementation is as follows: Data(Entity)->"data"->another Entity I originally thought I could decrypt the Data object first, extract a type from it, and then use that type to decrypt the necessary type from the String "d ...

An uncomplicated problem with a kendo-grid involving JSON

Seeking assistance with displaying my REST JSON data in a kendo-grid. After struggling for hours, I discovered that the issue lies with additional nodes in my JSON. $(function() { var grid = $("#grid").kendoGrid({ dataSource: { dat ...

Confirming if a JSON is valid in the C programming language

After hours of searching on Google, I am eager to find a way to programmatically verify if the string provided below is valid JSON using C. Is there any solution available? (I am currently relying on the json-c library.) char * jsonString = "{ ...

Changing multiple PHP variables into a JSON object

My PHP script has multiple variables: $name = "John"; $age = 30; $city = "New York"; // and more... Is there a way to combine these variables into a single JSON object? Can this be achieved in PHP? ...

Having trouble displaying information in JavaScript after using getScript() to retrieve data from an API containing a JSON block database

I'm a newcomer to the world of Ajax/json/jquery and I find myself with a few inquiries. Currently, I am working with an API located at which contains a JSON block that looks something like this [{"id":"1","FirstName":"Micheal","LastName":"Kooling"}, ...

Tips for validating JSON strings in PHP similar to the JSON_VALID function in MySQL

Is there a way to ensure that a JSON string is valid before inserting it into a MySQL database to avoid any JSON_VALID constraint violations? I've tried using json_decode with the following code: json_decode($string, true, 512, JSON_THROW_ON_ERROR); ...

How can I create a JSON object that contains a JSON array within it?

I am dealing with a many-to-many relationship structured as follows: Server version: 10.4.17-MariaDB table colors(id, name). table items(id, title....). table item_color(id, items_id, color_id). My SQL query looks like this: SELECT items.*, colors.name F ...

Ways to retrieve information from a intricate JSON structure?

Can anyone help me understand why I am unable to access the data in the detail option of the JSON? My goal is to load the firstName, lastName, and age into a list for each object. var data = { "events": [{ "date": "one", "event": "", "info ...

Node.js causing issues with retrieving data from REST Calls

I am trying to make a REST API call from my PHP and Node.js application to a specific URL provided by the client, which is expected to return a JSON object. While I am able to successfully retrieve data using PHP, I'm encountering issues with my Node ...

Encountering a syntax error: JSON.parse has found an unexpected end of data at the first line and first column of the JSON data

Having just started with angularJS, I was previously able to get the code below working without any issues. However, after some recent changes that I made, I am now encountering an error message stating: "JSON.parse: unexpected end of data at line 1 column ...

Retrieve the earliest and latest dates from a JSON file to utilize with FlatPicker

I have a file in an unknown format, possibly JSON, with dates listed. I am attempting to extract the minimum and maximum dates from this data in MM/DD/YYYY format for use as variables in Flatpicker's minDate and maxDate options. The current AJAX call ...

Error encountered in Android: PatternSyntaxException due to a syntax error caused by U_ILLEGAL_ARGUMENT_ERROR

I have been utilizing json-io to convert JSON data into Java objects: DataTransferContainer dataTransferContainer = (DataTransferContainer)JsonReader.jsonToJava(json); The DataTransferContainer simply holds some GeoJson elements. This detail is irrelevan ...