Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a file to an API - C#

Tags:

c#

I'm trying to use an API which sends a fax.

I have a PHP example below: (I will be using C# however)

<?php
//This is example code to send a FAX from the command line using the Simwood API
//It is illustrative only and should not be used without the addition of error checking etc.

$ch = curl_init("http://url-to-api-endpoint");
$fax_variables=array(
'user'=> 'test',
'password'=> 'test',
'sendat' => '2050-01-01 01:00',
'priority'=> 10,
'output'=> 'json',
'to[0]' => '44123456789',
'to[1]' => '44123456780',
'file[0]'=>'@/tmp/myfirstfile.pdf',
'file[1]' => '@/tmp/mysecondfile.pdf'
); 
print_r($fax_variables);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fax_variables); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
$info = curl_getinfo($ch);
$result['http_code'];
curl_close ($ch);
print_r($result);

?>

My question is - in the C# world, how would I achieve the same result?

Do i need to build a post request?

Ideally, i was trying to do this using REST - and constructing a URL, and using HttpWebRequest (GET) to call the API

like image 418
Alex Avatar asked Apr 08 '26 14:04

Alex


1 Answers

Anytime you are sending data you should use a POST. This is regardless of the technology involved. All of the standard http methods (POST, GET, PUT, DELETE) are supported by the idea of REST.

See this wiki entry.


UPDATE: For more info

There are lots of different options. One way, as you described, is to just use the HttpWebRequest object to craft the request and send it. Here's an example of posting data using that method (link), another is here.

An alternative method is to use WCF. Microsoft has some documentation on that here. And O'Reilly has a book on it here.

like image 149
NotMe Avatar answered Apr 10 '26 05:04

NotMe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!