2

I am trying to consume one WEB API in SSIS package.I want to post a pdf file in one server folder. I have created a Script task for that.

But when i execute that package, its giving me a some random runtime error.

byte[] content = System.IO.File.ReadAllBytes("Testfile.pdf");
string serviceUrl = "http://NNN.NN.NNN.NN:NNNN/";
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 = "api/UploadDocCon/PDA/Test.pdf/FlexField/ABC/ABCD?FolderName=FoldA";
HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;


if (response.IsSuccessStatusCode)
{
  var serviceResponse = response.Content.ReadAsAsync<DocumentServiceResponse>().Result as DocServiceResponse;
return serviceResponse.STREAMID;
}
else
{
string error = response.Content.ReadAsStringAsync().Result;
}

It gives me this error (its not even hitting the debugger)

Exception has been thrown by the target of an invocation

 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 
arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, 
Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags 
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, 
Binder binder, Object target, Object[] providedArgs, ParameterModifier[] 
modifiers, CultureInfo culture, String[] namedParams)
at  Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

when i replace this line

HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;

with var response = client.GetAsync(APIUrl).Result;

it does not give any error. But i want to post some byte array here.

What am i doing wrong here?

omkar patade
  • 1,442
  • 9
  • 34
  • 66

1 Answers1

2

You can transfer byte array using HttpClient by converting byte[] into System.Net.Http.HttpContent.

Here is the code snippet:

ByteArrayContent byteContent = new ByteArrayContent(content); HttpResponseMessage reponse = await client.PostAsJsonAsync(uri, byteContent);

Jeric Cruz
  • 1,899
  • 1
  • 14
  • 29