I'm trying to create an API call that will pass the parts of an address to an address verification service. The address must be passed as a single query parameter.
Using the following code
string input_address = HttpUtility.UrlEncode( string.Format( "{0} {1} {2} {3}",
location.Street1, location.Street2, location.City, location.PostalCode ) );
The query succeeds if all parts of the address is present however if multiple parts are missing then + signs accumulate and you end up with a query like.
/?api_key=####&query=++ID1+1QD&limit=1 which fails due to the extra '+' signs.
Is it possible to only pass values to a string if they are not null without using a set of if statements?
The full code is below:
string input_key = GetAttributeValue( "APIKey" );
string input_address = HttpUtility.UrlEncode( string.Format( "{0} {1} {2} {3}",
location.Street1, location.Street2, location.City, location.PostalCode ) );
var client = new RestClient("https://api.ideal-postcodes.co.uk/");
var request = new RestRequest( Method.GET );
request.RequestFormat = DataFormat.Json;
request.Resource = "v1/addresses/";
request.AddParameter("api_key", input_key);
request.AddParameter("query", input_address);
request.AddParameter("limit", "1");
request.AddHeader("Accept", "application/json");
var response = client.Execute( request );