Uwaa/HTTP/HttpContent.cs

45 lines
1 KiB
C#
Raw Normal View History

2024-11-22 07:40:43 +01:00
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace Uwaa.HTTP;
/// <summary>
/// Content served as the body of a HTTP request or response.
/// </summary>
public struct HttpContent
{
public static implicit operator HttpContent?([NotNullWhen(false)] string? value)
{
return value == null ? null : new HttpContent("text/plain", value);
}
/// <summary>
/// The MIME type of the HTTP content.
/// </summary>
public MIMEType Type;
/// <summary>
/// The raw HTTP content body, as bytes.
/// </summary>
public byte[] Content;
/// <summary>
2024-12-15 09:01:50 +01:00
/// Converts the contents to a UTF-8 string.
2024-11-22 07:40:43 +01:00
/// </summary>
public string AsText => Encoding.UTF8.GetString(Content);
public HttpContent(MIMEType type, string body)
{
Type = type;
Content = Encoding.UTF8.GetBytes(body);
}
public HttpContent(MIMEType type, byte[] body)
{
Type = type;
Content = body;
}
public override string ToString() => Type.ToString();
}