Creating CURL Request ASP.NET

12,959

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.

Share:
12,959
TroySteven
Author by

TroySteven

PHP, C#, JavaScript, Classic ASP, and ASP.NET. His database experience includes both SQL Server and MySQL.

Updated on June 14, 2022

Comments

  • TroySteven
    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
    TroySteven almost 10 years
    I 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
    TroySteven almost 10 years
    Yes, 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
    TroySteven almost 10 years
    I 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
    FaizanHussainRabbani over 7 years
    @devin-rader is this answer still valid? Because I am getting 400 Bad Request
  • Devin Rader
    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
    FaizanHussainRabbani over 7 years
    System.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
    Robert Smith almost 5 years
    Does anyone know why it would get stuck on this line: var webresponse = (HttpWebResponse)webrequest.GetResponse();
  • Robert Smith
    Robert Smith almost 5 years
    Macshaw, do you know why it would get stuck on this line: var webresponse = (HttpWebResponse)webrequest.GetResponse();