how to post plain text to ASP.NET Web API endpoint?
Solution 1:
Actually it's a shame that web API doesn't have a MediaTypeFormatter
for plain text. Here is the one I implemented. It can also be used to Post content.
public class TextMediaTypeFormatter : MediaTypeFormatter
{
public TextMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var taskCompletionSource = new TaskCompletionSource<object>();
try
{
var memoryStream = new MemoryStream();
readStream.CopyTo(memoryStream);
var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
taskCompletionSource.SetResult(s);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
return taskCompletionSource.Task;
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, System.Net.TransportContext transportContext, System.Threading.CancellationToken cancellationToken)
{
var buff = System.Text.Encoding.UTF8.GetBytes(value.ToString());
return writeStream.WriteAsync(buff, 0, buff.Length, cancellationToken);
}
public override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override bool CanWriteType(Type type)
{
return type == typeof(string);
}
}
You need to "register" this formatter in your HttpConfig by something like that:
config.Formatters.Insert(0, new TextMediaTypeFormatter());
Solution 2:
Since Web API doesn't have out of box formatter for handling text/plain, some options:
Modify your action to have no parameters... reason is having parameters triggers request body de-serialization. Now you can read the request content explicitly by doing
await Request.Content.ReadAsStringAsync()
to get the stringWrite a custom MediaTypeFormatter to handle 'text/plain'... it's actually simple to write in this case and you could keep the parameters on the action.
Solution 3:
In ASP.NET Core 2.0 you simply do the following :-
using (var reader = new StreamReader(Request.Body))
{
string plainText= reader.ReadToEnd();
// Do something else
return Ok(plainText);
}
Solution 4:
Purified version using of gwenzek's formatter employing async/await:
public class PlainTextFormatter : MediaTypeFormatter
{
public PlainTextFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override bool CanReadType(Type type) =>
type == typeof(string);
public override bool CanWriteType(Type type) =>
type == typeof(string);
public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var streamReader = new StreamReader(readStream);
return await streamReader.ReadToEndAsync();
}
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
{
var streamReader = new StreamWriter(writeStream);
await streamReader.WriteAsync((string) value);
}
}
Please note I intentionally do not dispose StreamReader/StreamWriter, as this will dispose underlying streams and break Web Api flow. See here:
"An implementation of this method should not close readStream upon completion. The stream will be closed independently when the HttpContent instance is disposed."
To make use of it, register while building HttpConfiguration
:
protected HttpConfiguration CreateHttpConfiguration()
{
HttpConfiguration httpConfiguration = new HttpConfiguration();
...
httpConfiguration.Formatters.Add(new PlainTextFormatter());
...
return httpConfiguration;
}
Solution 5:
In some situations it might be simpler to let the JsonMediaTypeFormatter let do the work:
var formatter = GlobalConfiguration.Configuration.Formatters.Where(f=>f is System.Net.Http.Formatting.JsonMediaTypeFormatter).FirstOrDefault();
if (!formatter.SupportedMediaTypes.Any( mt => mt.MediaType == "text/plain" ))
formatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue( "text/plain" ) );