Reputation: 51
The platform I am using forces me to send POST requests with a body of JSON format or XML format, so to test I am using SOAPUI so I can specify exactly the body I need for testing. I am POSTing to the URL https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxxxxxxxxxxx/Messages.json
with a JSON body of
{
"Body": "hi dear",
"To": "+1631xxxxxxx",
"From": "82xxx"
}
but it generates the error
{"code": 21604, "message": "A 'To' phone number is required.", "more_info": "https://www.twilio.com/docs/errors/21604", "status": 400}
but if the SOAP body is simple parameters
To=%2B1631xxxxxxx&From=82xxx&Body=Hi%20Dear
The message is send correctly and I get the Twilio response of
{
"sid": "SMxxxxxxxxxxxxxxxxxxxxx",
"date_created": "Tue, 06 Oct 2020 03:00:01 +0000",
"date_updated": "Tue, 06 Oct 2020 03:00:01 +0000",
...
}
Does Twilio have a messages API that I can POST with a formatted JSON or XML body.
Upvotes: 5
Views: 4942
Reputation: 1771
Your HTTP request needs to be a "form" request, not "json".
I agree that it's not perfectly documented, but it fixed the error for me after I learned that.
Upvotes: -1
Reputation: 73057
Twilio developer evangelist here.
The Twilio API only accepts requests with the body formatted as application/x-www-form-urlencoded
or simple parameters as you mention in your post. That is the only way to make requests to the Twilio API.
The core APIs will respond with XML (by default) or optionally JSON, CSV and HTML by appending the relevant suffix (.json
as you have included, .csv
or .html
). Newer APIs, anything under a subdomain like productname.twilio.com/v1
, only respond with JSON.
Twilio also provides helper libraries in 6 different languages, and there are community maintained libraries for many unsupported languages, that should abstract away the format of the request.
Let me know if that helps at all.
Upvotes: 3