Implementing an API endpoint that serves JSON data from an external API endpoint

We are going to implement an API endpoint, which takes some parameters and use these parameters to call an external endpoint then fetches the data as a response.

Let's say this is our projects domain https://myprojectsurl.com

I will add a controller namedArrivalsDeparturesDetails to handle this request.

My endpoint takes the following parameters airport, direction, year, month, day, hour

After implementing this controller, we should be sure it’s working by using an API testing tool. I prefer POSTMAN which is a great tool for this purpose. The URL that we need to use will be like below

https://myprojectsurl.com/arrivalsdeparturesdetails?airport=airportCode&direction=arrival&year=2018&month=11&day=28&hour=16

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

In my controller, I am sending a request to following external API endpoint https://externalapiurl.com/<airport>/<direction>/<year>/<month>/<day>/<hour>

To generate the appropriate endpoint URL in .NET, I will use string.Format

var url = "https://externalapiurl.com/{0}/{1}/{2}/{3}/{4}/{5}";
url = string.Format(CultureInfo.InvariantCulture, url, airport, direction, year, month, day, hour);

So, ArrivalsDeparturesDetailsmethod which handles the GET request will be as following:

[HttpGet]
public object ArrivalsDeparturesDetails(string airport, string direction, int year, int month, int day, int hour)
{
    var url = "https://externalapiurl.com/{0}/{1}/{2}/{3}/{4}/{5}";
url = string.Format(CultureInfo.InvariantCulture, url, airport, direction, year, month, day, hour);
    using (var w = new WebClient())
{
var json_data = string.Empty;
try
{
json_data = w.DownloadString(url);
}
        catch (Exception e)
{
LogHelper.Error(GetType(), e.Message, e);
return new { errorText = “Error occured” };
}
        return JsonConvert.DeserializeObject(json_data);
}
}

You may face The request was aborted: Could not create SSL/TLS secure channel error while downloading the JSON data from the external endpoint.

“errorText”: “The request was aborted: Could not create SSL/TLS secure channel.”

A possible cause of this error can be a mismatch between your client PC's configured cipher_suites values and the values that the server is configured as being willing and able to accept.

The error can be solved by adding necessary security protocols at the beginning of the controller:

// Necessary security protocols are added to be able to   downloadString url
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |     SecurityProtocolType.Tls | SecurityProtocolType.Tls11 |  SecurityProtocolType.Tls12;

I try to share the knowledge I gathered and the issues I have faced during the implementation of this API endpoint. I hope this article helps to clarify the questions in your mind about this topic.