Curl コマンドから http requestへ変更

ツール: https://reqbin.com/req/c-hlt4gkzd/curl-bearer-token-authorization-header-example


curl ‘https://api.xxxxxx/Messages.json’ -X POST \
–data-urlencode ‘To=+8100000000’ \
–data-urlencode ‘MessagingServiceSid=AAAAAAA’ \
–data-urlencode ‘Body=BBBBBB’ \
-u XXXXXX:YYYYYYYY

requestに変わるといかになります。

//APEXコード
//////String-> base64
Blob beforeblob = Blob.valueOf('XXXXXX:YYYYYYYY');
String basic = EncodingUtil.base64Encode(beforeblob); 

 HttpRequest req = new HttpRequest();
 req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 req.setHeader('Authorization', 'Basic '+ basic);
 req.setEndpoint('https://api.xxxxxx/Messages.json');
 req.setMethod('POST');

 String body = 'To='+EncodingUtil.urlEncode('+8100000000','UTF-8') 
   +'&MessagingServiceSid='+EncodingUtil.urlEncode('AAAAAAA','UTF-8')
   +'&Body='+EncodingUtil.urlEncode('BBBBBB','UTF-8');
 req.setBody(body);