add text/html content parser as string content (#5460)

* add text/html content parser as string content

* add text/html content parser in configuration

---------

Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com>
This commit is contained in:
jdevillard 2024-05-28 21:06:22 +02:00 committed by GitHub
parent ad236f626f
commit a130363a41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View file

@ -161,7 +161,8 @@ public class HttpFeature : FeatureBase
// Content parsers.
.AddSingleton<IHttpContentParser, JsonHttpContentParser>()
.AddSingleton<IHttpContentParser, XmlHttpContentParser>()
.AddSingleton<IHttpContentParser, PlainTextHttpContentParser>()
.AddSingleton<IHttpContentParser, PlainTextHttpContentParser>()
.AddSingleton<IHttpContentParser, TextHtmlHttpContentParser>()
// HTTP content factories.
.AddScoped<IHttpContentFactory, TextContentFactory>()

View file

@ -0,0 +1,24 @@
using Elsa.Http.Contracts;
namespace Elsa.Http.Parsers;
/// <summary>
/// Reads text/html content type streams.
/// </summary>
// TODO: found a library to use a Html Content Parser and use a complexe object Type, until this, this class allow to accept request send using text/html content-type
public class TextHtmlHttpContentParser : IHttpContentParser
{
/// <inheritdoc />
public int Priority => 0;
/// <inheritdoc />
public bool GetSupportsContentType(string contentType) => contentType.Contains("text/html", StringComparison.InvariantCultureIgnoreCase);
/// <inheritdoc />
public async Task<object> ReadAsync(Stream content, Type? returnType, CancellationToken cancellationToken)
{
using var reader = new StreamReader(content, leaveOpen: true);
var stringContent = await reader.ReadToEndAsync();
return stringContent;
}
}