I've created an ASP.NET Core web application, and installed + used the Swashbuckle.AspNetCore version 6.1.5 Nuget package. This hosts the following openapi document on https://example.com/swagger/v1/swagger.json.
Also my API supports content-negotiation.
- When sending no
Acceptheader, orAccept: text/xmlheader, the api will return an XML string - When sending an
Accept: application/jsonheader, the api will return a JSON string
Now I've tried consuming my api through the swagger document:
- Create a new .NET Core console application
- Right-click the project file → Add → Service Reference
- OpenAPI
- URL: https://example.com/swagger/v1/swagger.json
- Namespace: Example.Api
- Class name: ExampleClient
You can then write a Main like this:
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var httpClient = new System.Net.Http.HttpClient();
// httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var mintPlayerClient = new MintPlayer.Api.MintPlayerClient("https://mintplayer.com", httpClient);
var artists = await mintPlayerClient.ApiArtistListAsync(false);
}
Now when you debug the console app
- navigate to the
ApiArtistListAsyncmethod in theswaggerClientclass - put a breakpoint at the
client_.SendAsynccall - you can now inspect what the swaggerClient is sending to the webservice
Usually it's like this:
- SOAP = XML
- REST = JSON
Even when adding a DefaultRequestHeader on the HttpClient the response from the HttpClient is an XML, because it's explicitly added inside the SwaggerClient method:
And here is how the code is generated + the line where the Accept header is explicitly set (swaggerClient:430). This is auto-generated code from adding the service-reference.
So why is the Accept header in the generated code explicitly set to text/plain? Why isn't the default accept header value application/json, since this is a REST service?



I think you might want to check this on both side, client(your console) and server(your api project).
We all know that usually
But you're coding the whole things, total in-control of what being send and what being response.
Let's assume you client send Accept-Header which support both
text/xmlandtext/plain(which as i understand here, you expect a response astext/plain).Then the server realize that your console is happy with either
text/xmlandtext/plain, and the server itself support all kind of common format.So it'll have to electing the most convenient format to response to the client. Which in this case is
text/xml. That's so, the console received and happy withtext/xmlresponse format either wayAnd if that's the case, that I get you right, you want to receive
text/plainon the console, then make sure the onlyAcceptheader sending out istext/plainor do some custom logic on your API to choose thetext/plainformat over others when sending response.