DannyD
DannyD

Reputation: 2901

How can I get the If-Match header as a string using HttpRequestHeader

I'm building a http request in my c# client and and want to append an If-Match header. To get that If-Match header, I'm using this object which comes from System.Net:

var headerkey = HttpRequestHeader.IfMatch.ToString();

I would expect calling toString on this to output: If-Match however, instead I'm getting IfMatch which is not a valid http header. Is there a way I can get the correct value without hardcoding a string in my code that looks like this:

const string ifMatch = "If-Match";

Upvotes: 0

Views: 895

Answers (1)

Nathan Werry
Nathan Werry

Reputation: 896

You can use the code below, and it will generate the headers that you need based on those enums, instead of performing a ToString on an enum

var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
var headers = request.Headers;
headers.Add(HttpRequestHeader.IfMatch, "NameHere")

Upvotes: 1

Related Questions