I've already read and re-read the other posts here and here about cUrl to Apex. Those both dealt with only one parameter in the body of the request. This cUrl seems a bit trickier -- I can't seem to hack it.
I'm trying to make a call to Twilio's Notify API (allows you to send bulk messages in a single api call.) It works from cUrl, but I can't figure out how to parse it in Apex.
Here's the cUrl:
curl -X POST https://notify.twilio.com/v1/Services/[messageServiceId]/Notifications \
--data-urlencode "Body=Test Message from Pat." \
--data-urlencode 'ToBinding={"address": "+18005551212", "binding_type": "sms"}' \
--data-urlencode 'ToBinding={"address": "+18005551233", "binding_type": "sms"}' \
-u [my AccountId]:[my AuthToken]
And here's what I've been trying in Apex:
Http http = new Http();
HttpRequest request = new HttpRequest();
String AccountSid = [my AccountSid];
String AuthToken = [my AuthToken];
request.setMethod('POST');
Blob headerValue = Blob.valueOf(accountSID+':'+authToken);
String authorizationHeader = 'BASIC '+ EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
String body = 'Test message from Pat';
List<String> phoneNumbers = new List<String>();
String baseUrl = 'https://notify.twilio.com/v1/Services/';
String ServiceSid = [messageServiceId];
String NotificationsSuffix = '/Notifications';
request.setEndpoint(baseUrl + ServiceSid + NotificationsSuffix);
String payload = '';
payload += 'Body=' + body;
payload += '&ToBinding=[{"address":"+18005551212","binding_type":"sms"},{"address":"+18005551233","binding_type":"sms"}]');
request.setBody(payload);
HttpResponse response= http.send(request);
System.debug(response);
Note that "body" is one of the required parameters for the Twilio API -- it refers to the body of the message to be sent. Therefore, I've used the variable called payload for the body of the http request.
I'm not clear if I should be using urlEncode on the body elements, or how to concatenate them. Should I be using the question mark and/or ampersand?
Apex
, please include the[apex]
tag.