Skip to content

Commit b6a71e0

Browse files
committed
Use JsonContent when creating request bodies
1 parent 3246c7b commit b6a71e0

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@
220220
{% elsif operation.HasPlainTextBodyParameter -%}
221221
var content_ = new System.Net.Http.StringContent({{ operation.ContentParameter.VariableName }});
222222
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("{{ operation.Consumes }}");
223+
{% elsif UseSystemTextJson -%}
224+
var content_ = System.Net.Http.Json.JsonContent.Create({{ operation.ContentParameter.VariableName }}, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("{{ operation.Consumes }}"), {% if UseRequestAndResponseSerializationSettings %}Request{% endif %}JsonSerializerSettings);
223225
{% else -%}
224-
var json_ = {% if UseSystemTextJson %}System.Text.Json.JsonSerializer.SerializeToUtf8Bytes{% else %}Newtonsoft.Json.JsonConvert.SerializeObject{% endif %}({{ operation.ContentParameter.VariableName }}, {% if SerializeTypeInformation %}typeof({{ operation.ContentParameter.Type }}), {% endif %}{% if UseRequestAndResponseSerializationSettings %}Request{% endif %}JsonSerializerSettings);
225-
var content_ = new System.Net.Http.{% if UseSystemTextJson %}ByteArrayContent{% else %}StringContent{% endif %}(json_);
226+
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject({{ operation.ContentParameter.VariableName }}, {% if SerializeTypeInformation %}typeof({{ operation.ContentParameter.Type }}), {% endif %}{% if UseRequestAndResponseSerializationSettings %}Request{% endif %}JsonSerializerSettings);
227+
var content_ = new System.Net.Http.StringContent(json_);
226228
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("{{ operation.Consumes }}");
227229
{% endif -%}
228230
request_.Content = content_;

0 commit comments

Comments
 (0)