From c08388ec07cfc67386a18c91a8ca465109db84e3 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 24 Nov 2023 20:11:28 +0100 Subject: [PATCH] Update JsonContentFactory to use StringContent This fixes an issue that Content-Length header is not included when using JsonContent, which is problematic for certain backend APIs. --- .../Elsa.Http/ContentWriters/JsonContentFactory.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/Elsa.Http/ContentWriters/JsonContentFactory.cs b/src/modules/Elsa.Http/ContentWriters/JsonContentFactory.cs index 5d61f2fcf..a7983a913 100644 --- a/src/modules/Elsa.Http/ContentWriters/JsonContentFactory.cs +++ b/src/modules/Elsa.Http/ContentWriters/JsonContentFactory.cs @@ -2,6 +2,7 @@ using System.Net.Http.Headers; using System.Net.Http.Json; using System.Net.Mime; using System.Text; +using System.Text.Json; namespace Elsa.Http.ContentWriters; @@ -16,10 +17,11 @@ public class JsonContentFactory : IHttpContentFactory /// public HttpContent CreateHttpContent(object content, string contentType) { - if (content is string s) - return new StringContent(s, Encoding.UTF8, contentType); + var text = content as string ?? JsonSerializer.Serialize(content); - var mediaType = MediaTypeHeaderValue.Parse(contentType); - return JsonContent.Create(content, mediaType); + if (string.IsNullOrWhiteSpace(contentType)) + contentType = MediaTypeNames.Application.Json; + + return new StringContent(text, Encoding.UTF8, contentType); } } \ No newline at end of file