Using Twilio's curl with HttpClient

JassimRahmaJassimRahma USMember ✭✭✭✭
edited March 2018 in Xamarin.Forms

Hi,

I have below API from Twilio and I want to know how can utilize this curl using HttpClient so that I can use it in my app?

I need to pass parameters too, e.g. Body, From, To, etc..

curl 'https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxxxxxxxx529d6d/Messages.json' -X POST \
-u ACxxxxxxxxxxxxxxxxx529d6d:[AuthToken]

I tried below code but getting:

/Users/jassim/Projects/TwilioTest/TwilioTest/TwilioTestPage.xaml.cs(62,62): Error CS1729: 'AuthenticationHeaderValue' does not contain a constructor that takes 3 arguments (CS1729) (TwilioTest)

public async Task<HttpResponseMessage> SendReq()
{
    var accountSid = "zzzzzzzzzzz";
    var authToken = "xxxxxxxxxxx";
    var targeturi = "https://api.twilio.com/2010-04-01/Accounts/{0}/SMS/Messages";
    string from = "+0000000000";
    string to = "WoWSender";
    string message = "Test Message";

    var client = new System.Net.Http.HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", accountSid, authToken);
    // client.DefaultRequestHeaders.Authorization = CreateAuthenticationHeader("Basic", accountSid, authToken);

    var content = new StringContent(string.Format("From={0}&amp;To={1}&amp;Body={2}", from, to, message));
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

    var response = await client.PostAsync(string.Format(targeturi, accountSid), content);
    if (response.IsSuccessStatusCode)
    {
        //the POST succeeded, so update the UI accordingly
    }
    else
    {
        //the POST failed, so update the UI accordingly
    }
    return response;
}

Thanks,

Jassim

Sign In or Register to comment.