Beanymattd
Member
- Joined
- Oct 12, 2023
- Messages
- 6
- Programming Experience
- Beginner
Good morning,
I have some JSON format files and have a requirement to POST these to an API using C# inside SSIS, payloads are sent via HTTPS. The payloads are to be sent with the Content-Type header of "application/json" and the UserAPI-Key header.
Successful requests can return 200 and 201. Failures can return 400, 403, and 404. In the event of an error 400, the payload will continue, logging, where possible that this file failed. In the event of an error 403, or 404, the process will halt under an error condition.
Below is my JSON payload format to be sent (multiple files like this):
I have the following code which seemed like a good starting point, however i'm unsure due to my inepxerience if this is suitable and can be adapted to my requirements. Any help is greatly appreciated as always:
I have some JSON format files and have a requirement to POST these to an API using C# inside SSIS, payloads are sent via HTTPS. The payloads are to be sent with the Content-Type header of "application/json" and the UserAPI-Key header.
Successful requests can return 200 and 201. Failures can return 400, 403, and 404. In the event of an error 400, the payload will continue, logging, where possible that this file failed. In the event of an error 403, or 404, the process will halt under an error condition.
Below is my JSON payload format to be sent (multiple files like this):
C#:
{
"Time":"2018-10-21T00:08:03",
"Control_Code": "JKX",
"metrics": [
{
"Metric": 3721,
"Organisation":"AD450",
"Value": 20,
"Datetime":"2018-10-21T00:08:00"
},
{
"Metric": 1234,
"Organisation":"HG650",
"value": 88,
"datetime":"2018-10-21T00:08:00"
}
]
}
I have the following code which seemed like a good starting point, however i'm unsure due to my inepxerience if this is suitable and can be adapted to my requirements. Any help is greatly appreciated as always:
C#:
public void Main()
{
string response = "";
string json = Dts.Variables["User::FileName"].Value.ToString();
string url = "https://your.url.com";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Headers.Add("Authorization", "Basic your-auth-info");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 600000;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
response = streamReader.ReadToEnd();
}
httpResponse.Close();
}