1

I have this simple action

public void Post([FromBody] string t)
{
    var test = t;
}

I am trying to do a post through postman with a body from "this is a simple string" (note my text will be a lot longer so I want to do it through body not query).

I get this error

{
  "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
  "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'.",
  "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
  "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 1
    The rror message states "The request entity's media type 'text/plain'". Either change your service to accept it or ask Postman to send it in a different media type such as JSON. The latter one will be the easier solution as it looks like you're playing with standard ASP.NET Web APIs and their default is JSON. – Quality Catalyst Jan 05 '17 at 23:56
  • If I use json(what I usually do) I will need proper json format. I am trying to just send in a csv file of data. – chobo2 Jan 05 '17 at 23:59
  • You then have to accept CSV files. If you are doing a Web.API and you need files to be send, you better accept a `Stream`, not a `string`. – Quality Catalyst Jan 06 '17 at 00:01
  • how do you accept a stream? You have any examples? – chobo2 Jan 06 '17 at 00:02
  • This wasn't your question ;-) Search the internet please. There are loads of tutorials out there. Here's an SO question/answer to this topic and good sample code: http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi – Quality Catalyst Jan 06 '17 at 00:04
  • Also, the length of your string doesn't define whether it's a query or a body, the Web method you use defines what you should use. Get your head around HTTP verbs and possibly what REST is. You gonna like and need it. – Quality Catalyst Jan 06 '17 at 00:05
  • Check on it: [Configure in WebApi Config](http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome) for JSON. – Elek Guidolin Jan 06 '17 at 21:57

2 Answers2

0

The error message states "... The request entity's media type 'text/plain'". Either change your service to accept it or ask Postman to send it in a different media type such as JSON. The latter one will be the easier solution as it looks like you're playing with standard ASP.NET Web APIs and their default is JSON.

Send this data:

{ "t": "this is a simple string" }

... and you'll be sweet.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
0

As @Quality Catalyst stated, you can change request content type and send string in json format. Or you can add text/plain media type formatter:

public class PlainTextMediaTypeFormatter : MediaTypeFormatter
{
    public PlainTextMediaTypeFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
    }

    public override bool CanReadType(Type type)
    {
        return type == typeof(string);
    }

    public override bool CanWriteType(Type type)
    {
        return false; // you can return true and override WriteToStreamAsync
    }

    public override Task<object> ReadFromStreamAsync(Type type,
        Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
        CancellationToken cancellationToken)
    {
        var memoryStream = new MemoryStream();
        readStream.CopyTo(memoryStream);
        return Task.FromResult((object)Encoding.UTF8.GetString(memoryStream.ToArray()));
    }     
}

And register this formatter in WebApiConfig:

config.Formatters.Add(new PlainTextMediaTypeFormatter());

After that you will be able to send strings as plain text in request body.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459