From 4f000c641b0834470a1db646b7c7767090ae5c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrkan=20G=C3=BCran?= Date: Wed, 18 Jan 2023 13:00:27 +0100 Subject: [PATCH] Added content-type and headers options for HttpResponse activity --- .../Elsa.Http/Activities/WriteHttpResponse.cs | 28 +++++++++++++++++-- .../Elsa.Http/Models/HttpRequestHeaders.cs | 5 ++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs b/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs index 019eb98b93..f8e7d9d490 100644 --- a/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs +++ b/src/modules/Elsa.Http/Activities/WriteHttpResponse.cs @@ -1,7 +1,12 @@ using System.Net; +using System.Net.Http.Headers; +using Elsa.Extensions; using Elsa.Workflows.Core.Attributes; using Elsa.Workflows.Core.Models; +using Elsa.Workflows.Management.Models; using Microsoft.AspNetCore.Http; +using HttpRequestHeaders = Elsa.Http.Models.HttpRequestHeaders; +using HttpResponseHeaders = Elsa.Http.Models.HttpResponseHeaders; namespace Elsa.Http; @@ -23,6 +28,22 @@ public class WriteHttpResponse : Activity [Input(Description = "The content to write back.")] public Input Content { get; set; } = new(""); + /// + /// The content type to use when returning the response. + /// + [Input( + Description = "The content type to use when returning the response.", + Options = new[] { "", "text/plain", "text/html", "application/json", "application/xml", "application/x-www-form-urlencoded" }, + UIHint = InputUIHints.Dropdown + )] + public Input ContentType { get; set; } = default!; + + /// + /// The headers to return along with the response. + /// + [Input(Description = "The headers to return along with the response.", Category = "Advanced")] + public Input ResponseHeaders { get; set; } = new(new HttpResponseHeaders()); + /// protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) { @@ -39,11 +60,14 @@ protected override async ValueTask ExecuteAsync(ActivityExecutionContext context } var response = httpContext.Response; - response.StatusCode = (int)context.Get(StatusCode); + + var headers = ResponseHeaders.TryGet(context) ?? new HttpResponseHeaders(); + foreach (var header in headers) + response.Headers.Add(header.Key, header.Value); + response.ContentType = ContentType.TryGet(context); var content = context.Get(Content); - if (content != null) await response.WriteAsync(content, context.CancellationToken); } diff --git a/src/modules/Elsa.Http/Models/HttpRequestHeaders.cs b/src/modules/Elsa.Http/Models/HttpRequestHeaders.cs index 0c7b7cef88..0a205f19d9 100644 --- a/src/modules/Elsa.Http/Models/HttpRequestHeaders.cs +++ b/src/modules/Elsa.Http/Models/HttpRequestHeaders.cs @@ -3,6 +3,11 @@ namespace Elsa.Http.Models; public class HttpRequestHeaders : Dictionary +{ + public string? ContentType => this.GetValue("content-type")?[0]; +} + +public class HttpResponseHeaders : Dictionary { public string? ContentType => this.GetValue("content-type")?[0]; } \ No newline at end of file