question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

C# generated client using FormUrlEncodedContent instead of StringContent

See original GitHub issue

I 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:open
  • Created 2 years ago
  • Reactions:20
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
vincenzolaurettacommented, Apr 26, 2021

Hi, hopefully this helps someone who has this issue on Asp.net (.NET Framework)

I added the following custom filter class:

    internal class AssignContentTypeFilter : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            operation.consumes.Clear();
            operation.consumes.Add("application/json");

            operation.produces.Clear();
            operation.produces.Add("application/json");
        }
    }

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

7reactions
igorvoronacommented, Jun 22, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found