C# generated client using FormUrlEncodedContent instead of StringContent
See original GitHub issueI am working with an API that generate a specification like this:
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "SwaggerTest"
},
"host": "localhost:44386",
"schemes": [
"https"
],
"paths": {
"/api/Values": {
"post": {
"tags": [
"Values"
],
"operationId": "Values_Post",
"consumes": [
"application/json",
"text/json",
"application/xml",
"text/xml",
"application/x-www-form-urlencoded"
],
"produces": [],
"parameters": [
{
"name": "value",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"204": {
"description": "No Content"
}
}
}
}
},
"definitions": {}
}
This used to generate code like this:
var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(value, _settings.Value));
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
Today I updated NSwagStudio and now it generates code like this:
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(value, _settings.Value);
var dictionary_ = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, string>>(json_, _settings.Value);
var content_ = new System.Net.Http.FormUrlEncodedContent(dictionary_);
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
This looks like an error from the API since it should only accept application/json
but I do not have access to the API source code, if I manually delete the application/x-www-form-urlencoded
from consumes
, it works like before but I would like to understand if this change is a bug or if there is something I can configure on client-side to be able to generate the code as it was.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:20
- Comments:22 (1 by maintainers)
Top Results From Across the Web
C# generated client using FormUrlEncodedContent ...
I am working with an API that generate a specification like this: { "swagger": "2.0", "info": { "version": "v1", "title": "SwaggerTest" } ...
Read more >c# - HttpClient using FormUrlEncodedContent
You're going to need to create an HttpRequestMessage instance so that you can add the "Content-Type" header to it and use the HttpClient....
Read more >Send x-www-form-urlencoded Post Request Using ...
To send a post request, we should, first, create an object of the HttpClient class and use its PostAsync() method to send the...
Read more >Make HTTP requests with the HttpClient - .NET
To create an HttpClient , use the HttpClient class constructor. ... StringContent: Provides HTTP content based on a string.
Read more >C# HttpClient - creating HTTP requests with ...
C# HttpClient tutorial shows how to create HTTP requests with HttpClient in C#. In the examples, we create simple GET, HEAD, and POST...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi, hopefully this helps someone who has this issue on Asp.net (.NET Framework)
I added the following custom filter class:
then in SwaggerConfig.cs:
c.OperationFilter<AssignContentTypeFilter>();
reference link: https://stackoverflow.com/questions/58973329/asp-net-core-3-0-swashbuckle-restrict-responses-content-types-globally
Having the same issue with FormUrlEncodedContent generated instead of StringContent. It fails in controller’s endpoint unable to populate model class [FromBody]:
public async Task<IHttpActionResult> MyEndpoint([FromBody] MyModel model)
model is always null now. Why was the change, it was working fine in v13.10.8.0