Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "Content-Type = application/json;v=2.0" including versioning with HttpClient?

Sorry to ask this question like this. I can give you the feeling I would like you to do the code for me. I already spent a day tying to write the code to send a http request that contains a header with versioning:

For versioning I use the versioning by Media Type from Microsoft/aspnet-api-versioning project.

My API part is working fine and I can request the correct version without problem with Postman:

enter image description here

You see this line "Content-Type = application/json;v=2.0"? On postman no problem. With HttpClient from C# impossible to do.

Let me copy pas here all solution I tried with they error. I will Edit this question each time I try a new solution. By facility, but also to be more clear I will copy paste images:

Solution 1: The extension method with new Content = ...

enter image description here

Solution 1B: From BeginnerTejas

enter image description here

Solution 1C: From BeginnerTejas but using MediaTypeWithQualityHeaderValue now

enter image description here

Solution 2: The extension method with request.Headers.Add(...)

enter image description here

Solution 3: BaseProxy with Client.DefaultRequestHeaders.TryAddWithoutValidation(...) It seems I cannot add any "Content-Type"

enter image description here

Solution 4: BaseProxy with Client.DefaultRequestHeaders.Accept.Add(...)

enter image description here

Solution 5: request.Headers.TryAddWithoutValidation("Content-Type", "application/json");

enter image description here

like image 559
Bastien Vandamme Avatar asked Oct 18 '25 02:10

Bastien Vandamme


1 Answers

Can you try this?

request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue( "application/json" )
{
    Parameters = { new NameValueHeaderValue( "v", "2.0" ) }
};
like image 172
TejasGondalia Avatar answered Oct 20 '25 17:10

TejasGondalia