i want to call a http post request through my ssis package. But not sure what should be the best way of calling a rest api post method. Please help .
Asked
Active
Viewed 7,545 times
0
-
4Welcome to StackOverflow! What have you tried so far? – Alex Yu Feb 26 '19 at 13:58
-
Probably already answered here: https://stackoverflow.com/q/6684317/1531971 – Feb 26 '19 at 14:52
-
Possible duplicate of [How to make an HTTP request from SSIS?](https://stackoverflow.com/questions/6684317/how-to-make-an-http-request-from-ssis) – Hadi Feb 26 '19 at 22:20
2 Answers
1
You can use this method within a Script Task to make the api call using httpclient:
public void Post(Produto produto)
{
var client = new HttpClient;
client.BaseAddress = new Uri("https://localhost:5001/api/");
client.DefaultRequestHeaders.Clear();
//client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Add("ApiKey", "");
var jsonBody = JsonConvert.SerializeObject(produto);
client.PostAsync("createproduto", new StringContent(jsonBody, Encoding.UTF8, "application/json"));
}

Hadi
- 36,233
- 13
- 65
- 124

Fabrícia Santos
- 784
- 4
- 6
-1
You can make use of the namespace System.Net.WebClient to make the Http request with the help of Script Task in SSIS. Following example shows how this can be achieved. The example was created in SSIS 2008 R2.
public void Main()
{
Variables varCollection = null;
Dts.VariableDispenser.LockForRead("User::RemoteUri");
Dts.VariableDispenser.LockForRead("User::LocalFolder");
Dts.VariableDispenser.GetVariables(ref varCollection);
System.Net.WebClient myWebClient = new System.Net.WebClient();
string webResource = varCollection["User::RemoteUri"].Value.ToString();
string fileName = varCollection["User::LocalFolder"].Value.ToString() + webResource.Substring(webResource.LastIndexOf('/') + 1);
byte[] data;
using (WebClient client = new WebClient())
{
data = client.DownloadData(webResource);
}
FileInfo file = new System.IO.FileInfo(fileName);
file.Directory.Create(); // If the directory already exists, this method does nothing.
File.WriteAllBytes(file.FullName, data);
Dts.TaskResult = (int)ScriptResults.Success;
}

Rohit Gupta
- 443
- 3
- 17