+ Start a Discussion
Adam DrisselAdam Drissel 

Need a simple example of REST API being access via Apex

Alright...so the basics are this: I have scoured the blogs, stackexchange posts, and any other resource I could find looking for a simple way to access use the REST API to access object metadata.  I just cannot find anything.  Heck, I can't even get an access token to return.

Can anyone provide me with a minimal example of some code I can execute anonymously to receive an object's metadata?  This is the code I currently have, but if it is more complex than necessary please snip whatever is unnecessary out of your response(s).
 
String clientId = <my_client_id>;
String clientSecret = <my_client_secret>';
String username = <my_login>;
String password = <my_password>;
String prodUri = 'https://login.salesforce.com';
String testUri = 'https://test.salesforce.com';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(testUri+'/services/oauth2/token/');
req.setBody('grant_type=password' +
            '&client_id=' + clientId +
            '&client_secret=' + clientSecret +
            '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
            '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
Http binding = new Http();
HttpResponse res = binding.send(req);

system.debug('--------------------> '+res);
system.debug('--------------------> '+req);

The responses I am getting for any modifications I have attempted thus far have varied between 401 (unauthorized), 404 (not found), and 405 (method not available).  Please help.

Thanks!
ramesh kopparapu 15ramesh kopparapu 15
Hi Adam Drissel,

Object metadata can be accesseble easily with tooling api.
Here is the piece of code that i have used 

HttpRequest req = new HttpRequest();
req.setHeader('Authorization','Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
String toolingendpoint = System.Label.Toolingendpoint;
string customObjectId = 'xobject';
//query for custom objects
toolingendpoint += 'query/?q=select+id,DeveloperName+from+CustomObject+where+DeveloperName=\''+customObjectId+'\'';
req.setEndpoint(toolingendpoint);
req.setMethod('GET');
                
Http h = new Http();
HttpResponse res = h.send(req);

u will get json response with the above request, write a jsontoapex class.

Here  Toolingendpoint label contains url like: https://instance.salesforce.com/services/data/v33.0/tooling/
here instance like na10.
Adam DrisselAdam Drissel
Thanks for the quick response, Ramesh.  I am getting the following error:
Invalid external string name: toolingendpoint
What am I missing?

Also, is this allowing me to access Metadata as in field names, validation rule filter criteria, etc?
ramesh kopparapu 15ramesh kopparapu 15
toolingendpoint is the the label, value is https://instance.salesforce.com/services/data/v33.0/tooling/
here instance is like cs10, na10 like that.
And also create a remote site setting value as https://instance.salesforce.com

For getting field meta data
 HttpRequest req1 = new HttpRequest();
                    req1.setHeader('Authorization','Bearer ' + UserInfo.getSessionID());
                    req1.setHeader('Content-Type', 'application/json');
                    String toolingendpoint1 = system.Label.Toolingendpoint;
                    //query for custom fields
                    toolingendpoint1 += 'query/?q=select+id,DeveloperName,TableEnumOrId+from+CustomField+where+TableEnumOrId=\''+objectId+'\'';
                    req1.setEndpoint(toolingendpoint1);
                    req1.setMethod('GET');
                
                    Http h1 = new Http();
                    HttpResponse res1 = h1.send(req1);

Here objectId is object metadata which is getting erlier call.