function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VidhyaVidhya 

Calling SF existing Rest API's within apex.

I am trying to find ways to post Chatter private message to user.

 

Since there is no apex code directly to post Chatter private message to user, figured out there is a way in the REST APIs.

(Ref: http://www.salesforce.com/us/developer/docs/chatterapi/salesforce_chatter_rest_api.pdf)

/services/data/v24.0/chatter/users/me/messages?recipients=005D0000001GOYpIAO,
005D0000001GbdHIAS&Are+we+ready+for+next+week's+customer+meeting?

My question is, how do i call this in apex (Rest API's POST call)?

Since we are using internal salesforce rest api within the system, I am not sure if we should we use OAuth.

Can you please tell me how to call SF Rest api's using Apex?

 

Best Answer chosen by Admin (Salesforce Developers) 
ChrisOctagonChrisOctagon

Also, you don't have to perform OAuth, in Apex you already have a legal Session ID you can use instead. Add the following HTTP header to all your requests and they should work fine: 

Authorization: OAuth <session id>

All Answers

ChrisOctagonChrisOctagon

You can use the HttpRequest/HttpResponse classes in Apex to call out to REST. There is info near the bottom of here: http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts Make sure the site you call into is enabled in your Remote Sites settings. Apex has a new JSON processor you can use to process the JSON responses : http://developer.force.com/releases/release/Winter12/JSON+Support+in+Apex

ChrisOctagonChrisOctagon

Also, you don't have to perform OAuth, in Apex you already have a legal Session ID you can use instead. Add the following HTTP header to all your requests and they should work fine: 

Authorization: OAuth <session id>
This was selected as the best answer
VidhyaVidhya

Thanks Chris! That really helped.

 

But in Remote Site Settings we have to give our instance name like below:

https://<instancename>.salesforce.com

 

If i give generic test instance like below, it gives an error - Unauthorized Endpoint

https://test.salesforce.com

 

Is there any way/setting so that my Remote Site Setting can have the generic site name like https://test.salesforce.com

ChrisOctagonChrisOctagon

I believe that test.salesforce.com may just be a login server. When you actually log into it, are you redirected to a different server? If so, use that server instead.