Skip to content

Instantly share code, notes, and snippets.

@cwiederspan
Created September 9, 2016 23:47
Show Gist options
  • Select an option

  • Save cwiederspan/4851ecdd7c793cb480ea8fa190a7a15a to your computer and use it in GitHub Desktop.

Select an option

Save cwiederspan/4851ecdd7c793cb480ea8fa190a7a15a to your computer and use it in GitHub Desktop.
Quick snippet to allow plain text POST data to an ASP.NET Core Web Api
public class PlainTextFormatter : IInputFormatter {
public bool CanRead(InputFormatterContext context) {
return context.HttpContext.Request.ContentType.ToLower() == "text/plain";
}
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context) {
var reader = new StreamReader(context.HttpContext.Request.Body);
var content = reader.ReadToEnd();
return Task.FromResult(InputFormatterResult.Success(content));
}
}
public void ConfigureServices(IServiceCollection services) {
// Add framework services.
services.AddMvc(setup => {
var formatter = new PlainTextFormatter();
setup.InputFormatters.Add(new PlainTextFormatter());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment