Apex library for HTTP callouts.
You can use it with variety of request methods, authentication types or contexts.
Library is able to execute GET, POST, PUT, PATCH, DELET, HEAD. POST example:
Object result = new HttpClientAura(
new HttpClientProviderAuthProvider('SampleAuthProviderName'))
.post('https://foo.com'
+ '/v1/resource/baz/post'
, '{"fizz":"buzz"}');
Library supports several authentication schemes - AuthProvider, Named credentials, Cookie based authentication or basic username/password authentication, for example:
static Object postQuxByParams(String params){
return
new HttpClientAura(
new HttpClientProviderBasic('admin', 'hunter2'))
.post('https://foo.com'
+ '/v2/qux/post',
params);
}
Library provides basic catching of errors and reporting them via AuraHandledException that is readable on client side. Example call:
@AuraEnabled public static Object getFoo() {
return
new HttpClientAura(
new HttpClientProviderNamedCredentials('SampleNamedCredential'))
.get('https://foo.com'
+ '/v3/resource/foo/get');
}
new HttpClient(
new HttpClientProviderAuthProvider('SampleAuthProviderName'))
.get('https://foo.com'
+ '/v1/resource/foo/get');
final static String endpoint = '/services/data/';
final static String apiVersion = 'v43.0';
final static String query = '/tooling/query?q=';
final static String param = 'SELECT Description, EndpointUrl, FullName, IsActive, ManageableState, NamespacePrefix, ProtocolMismatch, SiteName, Metadata FROM RemoteProxy';
final static String sobjectApi = '/tooling/sobjects/RemoteProxy/';
@AuraEnabled public static Object Query(){
HttpClientProviderSelf self = new HttpClientProviderSelf();
return new HttpClientAura(self)
.get(self.getBaseUrl() + endpoint + apiVersion + query + EncodingUtil.urlEncode(param, 'UTF-8'));
}
See more at https://github.com/metacursion/RemoteSitesService
![Deploy to Salesforce](https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png)