Alex
Alex

Reputation: 38529

Sending a file to an API - 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

Upvotes: 3

Views: 1164

Answers (3)

code4life
code4life

Reputation: 15794

Based on the published specs, the HttpWebRequest object is what you should be using to communicate with the Simwood api using C#.

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88082

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.

Upvotes: 1

Simon Chadwick
Simon Chadwick

Reputation: 1148

I have been interacting with HTTP/S URLs from C# by invoking the Curl utility with the required command line parameters. See this SO answer for more guidance. Curl is purpose-built for this kind of requirement, is tried and true, and can be used from the command-line and batch files to test URLs and Post fields. Let Curl do what it's best at, and leave your C# code for your business logic.

Upvotes: 0

Related Questions