Click here to Skip to main content
15,885,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting the above error and I'm not sure how to resolve it.

I have the following in my model...

public class PUpdate
{
    public int customer_urn { get; set; }
    public int amount { get; set; }
    public string update_type { get; set; }
}

public class CreateUpdate : PUpdate
{
}


and when I try and run a POST test using Postman I get with above error.

The controller is

        // POST api/<controller>
        public string Post([FromBody]CreateUpdate value)
        {
            _conPU = new SqlConnection(ConfigurationManager.ConnectionStrings["KRConnectionString"].ConnectionString);

            var query = "insert into KR.L_AMT (ACUID, AMT, aDATE ,COMMENTS) values (@ACUID, 
@AMT, getdate() ,'Web')";
                SqlCommand insertcommand = new SqlCommand(query, _conPU);
                //insertcommand.Parameters.AddWithValue("@ACUID", value.customer_urn );
            //insertcommand.Parameters.AddWithValue("@AMT",value.amount );
            _conPU.Open();
            int result = insertcommand.ExecuteNonQuery();
            if (result > 0)
            {
                return "inserted";
            }
            else
            {
                return "failed";
            }
        }


I've also got the following in my webapiconfig.cs

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;

config.Formatters.Remove(config.Formatters.XmlFormatter);


The full error I get back is...

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'CreateUpdate' from content with media type 'text/plain'.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Tracing.Tracers.FormatterParameterBindingTracer.<>c__DisplayClass3.<ExecuteBindingAsync>b__1()\r\n   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEndAsync(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Func`1 execute, Action`1 endTrace, Action`1 errorTrace)\r\n   at System.Web.Http.Tracing.Tracers.FormatterParameterBindingTracer.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)\r\n   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n   at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"
}


Any pointers to what I'm doing wrong would be grateful. Thanks.

What I have tried:

I've tried changing to only having one variable in the json but still get the same error. My variables need to be of both type int and string.
Posted
Updated 14-Aug-20 0:45am
Comments
Sandeep Mewara 30-Jul-20 23:03pm    
Try having these proxy settings:

var handler = new HttpClientHandler(){
Proxy = HttpWebRequest.GetSystemWebProxy()
};

client = new HttpClient(handler);
Sandeep Mewara 30-Jul-20 23:06pm    
BTW, you have just mentioned:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));

Would suggest to add 'text/plain' too to the supported types to see what exactly is being returned back to make sure the response if as expected and nothing is wrong in there.
Bullgill Coder 31-Jul-20 3:29am    
Thanks, just adding "text/plain" here seems to have resolved the issue. Using postman to test it is now writing data into the database.
Sandeep Mewara 31-Jul-20 3:52am    
Cool! Do tell if this works!

1 solution

just adding "text/plain" here seems to have resolved the issue. Using postman to test it is now writing data into the database.
 
Share this answer
 
Comments
Member 10587992 20-Nov-22 5:39am    
How i do this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900