Creating CURL Request ASP.NET
Twilio evangelist here.
Just to make sure we are on the same page, you need to make a POST request to theMessages endpoint in the Twilio API, but you cannot use our helper library.
Not a problem, you can just use .NETs native HTTP client libraries, HttpWebRequest and HttpWebResponse. Thats going to look something like this:
//Twilio Credentials
string accountsid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string authtoken = "asdsadasdasdasdasdsadsaads";
//Twilio API url, putting your AccountSid in the URL
string urltemplate = "https://api.twilio.com/2010-04-01/Accounts/{0}/Messages.json";
string url = string.Format(urltemplate, accountsid);
//Create a basic authorization
string basicauthtoken = string.Format("Basic {0}", System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(accountsid + ":" + authtoken)));
//Build and format the HTTP POST data
string formencodeddata = "To=+15555555555&From=+15556666666&Body=Hello World";
byte[] formbytes = System.Text.ASCIIEncoding.Default.GetBytes(formencodeddata);
//Create a new HTTP request object, set the method to POST and write the POST data to it
var webrequest = (HttpWebRequest)WebRequest.CreateHttp(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.Headers.Add("Authorization", basicauthtoken);
using (Stream postStream = webrequest.GetRequestStream()) {
postStream.Write(formbytes, 0, formbytes.Length);
}
//Make the request, get a response and pull the data out of the response stream
var webresponse = (HttpWebResponse)webrequest.GetResponse();
Stream responseStream = webresponse.GetResponseStream();
var reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();
There are also async versions of the GetRequestStream and GetResponse methods if you need them.
Hope that helps.
TroySteven
PHP, C#, JavaScript, Classic ASP, and ASP.NET. His database experience includes both SQL Server and MySQL.
Updated on June 14, 2022Comments
-
TroySteven almost 2 years
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/AC053acaaf55d75ef32233132196e/Messages.json' \ --data-urlencode 'To=5555555555' \ --data-urlencode 'From=+15555555555' \ --data-urlencode 'Body=Test' \ -u AC053acaaf55d75a393498192382196e:[AuthToken]
I have the above curl code for an API I need to connect to. The problem is I need to connect using ASP.NET (C#). I'm not very familiar with ASP.NET and don't really know where to begin. I know how to code this in PHP but ASP.NET is another matter. From the research I've done I need to use WebRequest. How do I feed in the post data and the authtoken (-u AC053acaaf55d75a393498192382196e:[AuthToken]) part of the request.
string url = "https://api.twilio.com/2010-04-01/Accounts/AC053acaaf55d75ef32233132196e/Messages.json"; WebRequest myReq = WebRequest.Create(url); myReq.Method = "POST";
-
TroySteven almost 10 yearsI actually can't connect to Twilio C# api. For this particular project I am working on I cannot add anymore Dlls so I need to connect to their cURL api directly.
-
TroySteven almost 10 yearsYes, that is what I referring too. However, when I run the code I get a 400 bad request error. Not sure what's wrong.
-
TroySteven almost 10 yearsI got it working, I just had to add webrequest.ContentType = "application/x-www-form-urlencoded"; after webrequest.Method = "POST"; If you edit your code I'll mark it as answered.
-
FaizanHussainRabbani over 7 years@devin-rader is this answer still valid? Because I am getting 400 Bad Request
-
Devin Rader over 7 years@FaizanRabbani yes, this is still valid. The body of the 400 response should have details on what was bad about the request.
-
FaizanHussainRabbani over 7 yearsSystem.Text.ASCIIEncoding.Default.GetBytes is omitting "+" symbol, due to which I was getting not valid To number error, I had to use HttpUtility.UrlEndcode to properly encode + symbol to %2B
-
Robert Smith almost 5 yearsDoes anyone know why it would get stuck on this line: var webresponse = (HttpWebResponse)webrequest.GetResponse();
-
Robert Smith almost 5 yearsMacshaw, do you know why it would get stuck on this line: var webresponse = (HttpWebResponse)webrequest.GetResponse();