Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Add Accept header to HttpClient

What is the difference between these two calls? My end goal is to have Accept: application/json sent over the wire, not to append to some default set of other MIME types.

HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/json"); 

vs.

client.DefaultRequestHeaders   .Accept   .Add(new MediaTypeWithQualityHeaderValue("application/json")); 

My CLR is .NET Core 2.0.

Sniffing the wire reveals no difference:

# just .Add("Accept"... ~ % nc -l 8000 GET / HTTP/1.1 Connection: Keep-Alive Accept: application/json [...]  # with MediaTypeWithQualityHeaderValue ~ % nc -l 8000 GET / HTTP/1.1 Connection: Keep-Alive Accept: application/json [...] 

So, outside the bizarre naming of that type, nothing else to gain here right?

like image 296
evilSnobu Avatar asked Nov 08 '17 09:11

evilSnobu


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

There is no difference.

DefaultRequestHeaders.Accept is a collection of string type, where you can add your header to accept using the new instance of MediaTypeWithQualityHeaderValue.

client.DefaultRequestHeaders is a dictionary that accepts key for and value for the request header and matches the results according to them.

DefaultRequestHeaders 

has overloads.

The only thing that differs between them, is the fact that DefaultRequestHeaders.Accept will require you to initialize a new instance of MediaTypeWithQualityHeaderValue class, resulting in another reference type in the heap, while client.DefaultRequestHeaders will add the data to the dictionary, removing the cost of resources and the need to initialize a new instance.

It is really up to the user as to how and what to use.

like image 150
Barr J Avatar answered Sep 17 '22 19:09

Barr J


There's no difference in the end result, as long as the names and values are correct.

The HTTP standard specifies that certain headers have a quality factor, hence the name MediaTypeWithQualityHeaderValue. It's a MediaType header value that can have a Quality factor. You can pass the quality factor if you use the MediaTypeWithQualityHeaderValue Constructor (String, Double) constructor

The Accept header section in the standard shows several examples that use the quality factor. For example,

The example

   Accept: audio/*; q=0.2, audio/basic 

SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80% mark-down in quality."

You could write that with

var requestAccepts=client.DefaultRequestHeaders.Accept; requestAccepts.Add(new MediaTypeWithQualityHeaderValue("audio/*",0.2)); requestAccepts.Add(new MediaTypeWithQualityHeaderValue("audio/basic")); 

Or you can enter the raw header value with :

client.DefaultRequestHeaders.Add("Accept", "audio/*; q=0.2, audio/basic"); 
like image 28
Panagiotis Kanavos Avatar answered Sep 18 '22 19:09

Panagiotis Kanavos