Skip to content

Commit

Permalink
Added content-type and headers options for HttpResponse activity
Browse files Browse the repository at this point in the history
  • Loading branch information
grkngrn committed Jan 18, 2023
1 parent f5f5c71 commit 4f000c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/modules/Elsa.Http/Activities/WriteHttpResponse.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,6 +28,22 @@ public class WriteHttpResponse : Activity
[Input(Description = "The content to write back.")]
public Input<string?> Content { get; set; } = new("");

/// <summary>
/// The content type to use when returning the response.
/// </summary>
[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<string?> ContentType { get; set; } = default!;

/// <summary>
/// The headers to return along with the response.
/// </summary>
[Input(Description = "The headers to return along with the response.", Category = "Advanced")]
public Input<HttpResponseHeaders?> ResponseHeaders { get; set; } = new(new HttpResponseHeaders());

/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/modules/Elsa.Http/Models/HttpRequestHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Elsa.Http.Models;

public class HttpRequestHeaders : Dictionary<string, string[]>
{
public string? ContentType => this.GetValue("content-type")?[0];
}

public class HttpResponseHeaders : Dictionary<string, string[]>
{
public string? ContentType => this.GetValue("content-type")?[0];
}

0 comments on commit 4f000c6

Please sign in to comment.