@@ -1007,6 +1007,9 @@ public static async Task<HttpResponseMessage> PutFileToUrlAsync(this string url,
10071007 method : HttpMethods . Post , requestFilter : requestFilter , responseFilter : responseFilter , token : token ) . ConfigAwait ( ) ;
10081008 }
10091009
1010+ public static void AddHeader ( this HttpRequestMessage res , string name , string value ) =>
1011+ res . WithHeader ( name , value ) ;
1012+
10101013 public static string ? GetHeader ( this HttpRequestMessage res , string name ) =>
10111014 res . Headers . TryGetValues ( name , out var values ) ? values . FirstOrDefault ( ) : null ;
10121015
@@ -1020,7 +1023,13 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
10201023 httpReq . Headers . Authorization =
10211024 new AuthenticationHeaderValue ( value . LeftPart ( ' ' ) , value . RightPart ( ' ' ) ) ;
10221025 }
1023- else if ( name . Equals ( HttpHeaders . UserAgent ) )
1026+ else if ( name . Equals ( HttpHeaders . ContentType , StringComparison . OrdinalIgnoreCase ) )
1027+ {
1028+ if ( httpReq . Content == null )
1029+ throw new NotSupportedException ( "Can't set ContentType before Content is populated" ) ;
1030+ httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( value ) ;
1031+ }
1032+ else if ( name . Equals ( HttpHeaders . UserAgent , StringComparison . OrdinalIgnoreCase ) )
10241033 {
10251034 if ( value . IndexOf ( '/' ) >= 0 )
10261035 {
@@ -1030,7 +1039,7 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
10301039 }
10311040 else
10321041 {
1033- // Avoid format excetions
1042+ // Avoid format exceptions
10341043 var commentFmt = value [ 0 ] == '(' && value [ ^ 1 ] == ')' ? value : $ "({ value } )";
10351044 httpReq . Headers . UserAgent . Add ( new ProductInfoHeaderValue ( commentFmt ) ) ;
10361045 }
@@ -1042,22 +1051,24 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
10421051 return httpReq ;
10431052 }
10441053
1045- public static HttpRequestMessage With ( this HttpRequestMessage httpReq ,
1046- string ? accept = null ,
1047- string ? userAgent = null ,
1048- KeyValuePair < string , string > ? authorization = null ,
1049- Dictionary < string , string > ? headers = null )
1054+ public static HttpRequestMessage With ( this HttpRequestMessage httpReq , Action < HttpRequestConfig > configure )
10501055 {
1051- headers ??= new Dictionary < string , string > ( ) ;
1052-
1053- if ( accept != null )
1054- headers [ HttpHeaders . Accept ] = accept ;
1055- if ( userAgent != null )
1056- headers [ HttpHeaders . UserAgent ] = userAgent ;
1057- if ( authorization != null )
1056+ var config = new HttpRequestConfig ( ) ;
1057+ configure ( config ) ;
1058+
1059+ config . Headers ??= new Dictionary < string , string > ( ) ;
1060+ var headers = config . Headers ;
1061+
1062+ if ( config . Accept != null )
1063+ headers [ HttpHeaders . Accept ] = config . Accept ;
1064+ if ( config . UserAgent != null )
1065+ headers [ HttpHeaders . UserAgent ] = config . UserAgent ;
1066+ if ( config . ContentType != null )
1067+ headers [ HttpHeaders . ContentType ] = config . ContentType ;
1068+ if ( config . Authorization != null )
10581069 httpReq . Headers . Authorization =
1059- new AuthenticationHeaderValue ( authorization . Value . Key , authorization . Value . Value ) ;
1060-
1070+ new AuthenticationHeaderValue ( config . Authorization . Value . Key , config . Authorization . Value . Value ) ;
1071+
10611072 foreach ( var entry in headers )
10621073 {
10631074 httpReq . WithHeader ( entry . Key , entry . Value ) ;
@@ -1071,7 +1082,10 @@ public static void DownloadFileTo(this string downloadUrl, string fileName,
10711082 {
10721083 var client = Create ( ) ;
10731084 var httpReq = new HttpRequestMessage ( HttpMethod . Get , downloadUrl )
1074- . With ( accept : "*/*" , headers : headers ) ;
1085+ . With ( c => {
1086+ c . Accept = "*/*" ;
1087+ c . Headers = headers ;
1088+ } ) ;
10751089
10761090 var httpRes = client . Send ( httpReq ) ;
10771091 httpRes . EnsureSuccessStatusCode ( ) ;
0 commit comments