0

Org 2 has GET Method:

@RestResource(urlMapping='/jobShow/*')
global with sharing class RestJob {

    @HttpGet
    global static List<Job__c> getJob(){
        List<Job__c> jobList;

        try{
            jobList = [SELECT Description__c 
                          FROM Job__c];  
        } catch(Exception e){
            System.debug('Error: ' + e.getMessage());
        }
        return jobList;     
    } 
}

Org 1 has Remote Site with link to Org 2: https://***.lightning.force.com

Org 1 also has a Callout Method:

public class HttpCalloutJob {

    public String getCalloutResponseContents(String url) {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://***.lightning.force.com/services/apexrest/jobShow');  //I don't know: is it right Link or not?
    req.setMethod('GET');

    HttpResponse res = h.send(req);   
    System.debug('Body: ' + res.getBody());
    return res.getBody();     
  }
}

Then I try to Execute this part

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://***.lightning.force.com/services/apexrest/jobShow');
req.setMethod('GET');
HttpResponse res = h.send(req);      
System.debug('Body: ' + res.getBody());

in Execute Anonymous Window, and become empty Body. But from workbench all is ok: it's not empty.

I think my link is wrong or I didn't connect my Orgs. How can I fix it?

2 Answers 2

1

Are you passing an access token alongwith with request? You will need a token which will get you access to Org2. When you will make a request to the access token, you will get instance url returned as a part of the response as well.

The instance-url returned is the link/url that you will need to hit.

2
-2

You need to add the consumer key/ secret key along with endpoint URL

1
  • 1
    This is not correct (or at the very least incomplete). You need to go through an OAuth 2.0 flow and get an access token (as suggested in the other answer). Going through the OAuth flow requires the consumer key (and most but not all flows require the consumer secret), and the access token is what you'd include in the request (as a header)
    – Derek F
    Commented Sep 10, 2021 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .