| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Http.ContentWriters; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A content writer that writes content in the application/x-www-form-urlencoded format. |
| | | 8 | | /// </summary> |
| | | 9 | | public class FormUrlEncodedHttpContentFactory : IHttpContentFactory |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | 40 | 12 | | public IEnumerable<string> SupportedContentTypes => ["application/x-www-form-urlencoded"]; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 0 | 15 | | public HttpContent CreateHttpContent(object content, string? contentType = null) => new FormUrlEncodedContent(GetCon |
| | | 16 | | |
| | | 17 | | private static IDictionary<string, string> GetContentAsDictionary(object content) |
| | | 18 | | { |
| | 0 | 19 | | if (content is IDictionary<string, object> dictionary) |
| | 0 | 20 | | return dictionary.ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | | 21 | | |
| | 0 | 22 | | if (content is string or JsonObject) |
| | 0 | 23 | | return JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(content))!; |
| | | 24 | | |
| | 0 | 25 | | var jsonElement = JsonSerializer.SerializeToElement(content); |
| | 0 | 26 | | return jsonElement.EnumerateObject().ToDictionary(x => x.Name, x => x.Value.ToString()); |
| | | 27 | | } |
| | | 28 | | } |