Calling a Restful API in SSIS and returning output

VickyD 81 Reputation points
2020-12-01T17:24:37.1+00:00

Hi All,

I have a requirement to call a restful API fetch the results. Request will be in xml form and so the response also.

I'm using VS 2019. Please let me know if you have any steps to implement this in c# script task or sample code.

Thanks in advance.

Vikram

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,453 questions
{count} votes

Accepted answer
  1. Monalv-MSFT 5,891 Reputation points
    2020-12-02T06:28:50.763+00:00

    Hi @Anonymous ,

     public void Main()      
            {      
                try      
                {      
                    string serviceUrl = Dts.Variables["$Project::ServiceUrl"].Value.ToString();      
                    HttpClient client = new HttpClient();      
                    client.BaseAddress = new Uri(serviceUrl);      
                    // Add an Accept header for JSON format.      
                    client.DefaultRequestHeaders.Accept.Add(      
                        new MediaTypeWithQualityHeaderValue("application/json"));      
          
                    decimal amount = 1200.50m;      
                    long tranactionID = 1001;      
                    string APIUrl = string.Format(serviceUrl + "/ProcessManager/ProcessData?amount={0}&transactionID={1}", amount, tranactionID);      
                    var response = client.GetAsync(APIUrl).Result;      
                    if (response.IsSuccessStatusCode)      
                    {      
                        var result = response.Content.ReadAsAsync<Response>().Result;      
                        if (result.IsSucess)      
                        {    
                            //TODO update your database based on the result      
                        }      
                        Dts.TaskResult = (int)ScriptResults.Success;      
                    }      
                }      
                catch (Exception ex)      
                {      
                    Dts.TaskResult = (int)ScriptResults.Failure;      
                }      
            }     
    

    Hope the following links will be helpful:

    1.How To Consume Web API Through SSIS Package

    2.Download source file from website with SSIS

    Best Regards,
    Mona

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues in November--What can I do if my transaction log is full?
    Hot issues in November--How to convert Profiler trace into a SQL Server table?


3 additional answers

Sort by: Most helpful
  1. COZYROC 101 Reputation points
    2020-12-06T22:25:46.117+00:00

    Hi Vikram,

    I would recommend you check the commercial COZYROC REST technology. You can use any REST API with it.


  2. Samuele Biasiolo 0 Reputation points
    2023-12-12T20:57:07.2333333+00:00

    This code works for me :

    public void Main()
    {
        // TODO: Add your code here
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        string url = "api url";
    
    
        try { 
         WebClient c = new WebClient();
        c.Headers.Add("Authorization", "password");
        c.DownloadFileTaskAsync(url, "C:\\temp\\pippo.csv").Wait();
        
    
        Dts.TaskResult = (int)ScriptResults.Success;
        }
    
        catch (Exception ex)
        {
            Dts.Events.FireError(0, "ERROR", ex.Message, null, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    
    }
    
    0 comments No comments

  3. pankaj (ZappySys Team) 0 Reputation points
    2024-02-13T11:38:41.59+00:00

    Hi there,

    Absolutely! With ZappySys Rest API Task/Component, you can seamlessly send API requests and efficiently manage both JSON and XML response data.

    For comprehensive instructions, I recommend referring to this tutorial:

    How to Call REST API in SSIS – Read JSON / XML / CSV

    This tutorial provides detailed guidance along with a helpful video tutorial. Feel free to explore it for a deeper understanding.

    0 comments No comments