11using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . Collections . Specialized ;
45using System . Net . Http ;
@@ -115,6 +116,9 @@ private void BuildQueryParameters()
115116 // add query parameters
116117 NameValueCollection query = HttpUtility . ParseQueryString ( string . Empty ) ;
117118
119+ // Separately store the list parameters to add them last
120+ List < string > listParameters = new ( ) ;
121+
118122 // build query string from parameters
119123 foreach ( KeyValuePair < string , object > param in _parameters )
120124 {
@@ -124,26 +128,52 @@ private void BuildQueryParameters()
124128 continue ;
125129 }
126130
127- query [ param . Key ] = param . Value switch
131+ var @switch = new SwitchCase
128132 {
129- // TODO: Handle special conversions for other types
130- // DateTime dateTime => dateTime.ToString("o", CultureInfo.InvariantCulture),
131- var _ => param . Value . ToString ( ) ,
133+ { param . Value is IList , ( ) => listParameters = AddListQueryParameter ( listParameters , param . Key , ( IList ) param . Value ) } ,
134+ { SwitchCaseScenario . Default , ( ) => query [ param . Key ] = param . Value . ToString ( ) } ,
132135 } ;
136+ @switch . MatchFirstTrue ( ) ;
133137 }
134138
135- // short circuit if no query parameters
136- if ( query . Count == 0 )
139+ // Finalize the query string
140+ string queryString = query . ToString ( ) ?? string . Empty ;
141+
142+ // Add list parameters to the query string
143+ string parameterCharacter = queryString . Length == 0 ? "?" : "&" ;
144+ foreach ( string pair in listParameters )
137145 {
138- return ;
146+ queryString += $ "{ parameterCharacter } { pair } ";
147+ parameterCharacter = "&" ;
139148 }
140149
141150 // rebuild the request URL with the query string appended
142151 var uriBuilder = new UriBuilder ( _requestMessage . RequestUri ! )
143152 {
144- Query = query . ToString ( ) ,
153+ Query = queryString ,
145154 } ;
146- _requestMessage . RequestUri = new Uri ( uriBuilder . ToString ( ) ) ;
155+
156+ // _requestMessage.RequestUri = new Uri(uriBuilder.ToString());
157+ _requestMessage . RequestUri = uriBuilder . Uri ;
158+ }
159+
160+ private static List < string > AddListQueryParameter ( List < string > pairs , string key , IList value )
161+ {
162+ string keyPrefix = $ "{ HttpUtility . UrlEncode ( key ) } []";
163+ // ReSharper disable once LoopCanBeConvertedToQuery
164+ foreach ( object ? item in value )
165+ {
166+ string ? itemString = item ? . ToString ( ) ;
167+ if ( itemString == null )
168+ {
169+ continue ;
170+ }
171+
172+ string pair = $ "{ keyPrefix } ={ HttpUtility . UrlEncode ( itemString ) } ";
173+ pairs . Add ( pair ) ;
174+ }
175+
176+ return pairs ;
147177 }
148178
149179 /// <inheritdoc cref="EasyPostClient._isDisposed"/>
0 commit comments