Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "A From phone number is required" in JSON call to Twilio

Tags:

json

rest

c#

twilio

I'm trying to use JSON over HTTP to communicate with the Twilio API (I'm running on asp.net vNext - and their C# library doesn't yet support coreclr)

I've got the following code, but every time I call the API I get the same 400 - Bad request response:

{"code": 21603, "message": "A 'From' phone number is required.", "more_info": "https://www.twilio.com/docs/errors/21603", "status": 400}

I'm using the following code to setup the request and make the call - the auth succeeds and I get the above error:

var byteArray = Encoding.ASCII.GetBytes("{AccountSid}:{AuthToken}");

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

var jsonStr = JsonConvert.SerializeObject(new {
    From = WebUtility.UrlEncode("+15005550003"), // A test number twilio supply
    To = WebUtility.UrlEncode("{my phone number}"),
    Body = WebUtility.UrlEncode("Hi there from a pure json rest client...")
});

var response = await client.PostAsync("https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/SMS/Messages.json", new StringContent(jsonStr));
like image 939
ry8806 Avatar asked Nov 17 '15 18:11

ry8806


People also ask

How do I add my phone number to SMS service Twilio?

Adding numbers to a Messaging Service using ConsoleOn the left-hand side, click the “Sender Pool” link for your Messaging Service, then click the “Add Senders” button on the right side of the page. If you want to add Twilio phone numbers, this option will be pre-selected in the dialog that appears. Click Continue.

Does Twilio give you a phone number?

Twilio's virtual phone numbers give you instant access to local, national, mobile, and toll-free phone numbers in over 100 countries for your voice call and messaging applications. Leverage local phone numbers for your customers to call and text, or use your own number.

Can I send SMS to any number using Twilio?

Yes, if your phone number is SMS-enabled, you can use it to send SMS messages. A list of countries where Twilio offers SMS-enabled phone numbers and their capabilities can be found here. Please note that local SMS-enabled phone numbers can only be used to send messages domestically.


1 Answers

After contacting support and dealing with a very helpful guy called Joshua, we have figured it out.

In Twilio docs you can either use a url with "messages.json" or "messages.xml" - I took that to mean: whichever url you use, the message (request and response) will have to follow that format.

Unfortunately that is not true - all requests to Twilio, must be of Content-Type: application/x-www-form-urlencoded

e.g. To=0123456789&From=9876543210&Body=Hi

in C# (HttpClient) that looks like:

_client.PostAsync(_url, new FormUrlEncodedContent(new Dictionary<string, string>(){
 { "To", "0123456789" }, 
 { "From", "9876543210" }, 
 { "Body", "Hi" } }));
like image 165
ry8806 Avatar answered Oct 12 '22 23:10

ry8806