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
sashamsasham 

Need a help on the get request apex class

our client is supposed to send us notification after the resuts has been reviewd. 
1) approval notification 
2)Results  are  availble
these are the urls they asked me to  set up in our end 
http://example.com/notif?id={id}&status=approved
http://example.com/notif?id={id}&status=resulted[&result_type={result}]

For that . I need to setup up an apex class as a REST endpoint for an HTTP GET request that an external system is sending notification.
Please le me know is the right way do or please guide me 
@RestResource(urlMapping='/sample/approvalnotifications')
global without sharing class  Rest_Approval_Notification {

    
    @HttpGet
    global static void doGet() {
        
        System.debug(RestContext.request.requestBody);
        restresponse res=restcontext.response;
        String notification_id = RestContext.request.params.get('id');
        String notification_status = RestContext.request.params.get('status');
        System.debug(notification_id);
        System.debug(notification_status);
		
		//hadling error code and logic for processing 
        
        
        
    }
        
}

 


 
Amit Singh 1Amit Singh 1
give it a try
@IsTest
private class MyFirstRestAPIClassTest{
    static testMethod void testGetMethod(){        
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/sample/approvalnotifications';
        request.httpMethod = 'GET';
        request.addParameter('id', '12324434');
        request.addParameter('status', 'Status Value Here');
        RestContext.request = request;
        Rest_Approval_Notification.doGet();
    }
}


 
sashamsasham
Hi Amit ,  Thank you .I cheked that using this test method and postmal as well. it seems woking 
I have question on this folowwing and how do i use the paarameter for this  [&result_type={result}]. parynot undestanding properly. 
http://example.com/notif?id={id}&status=resulted[&result_type={result}]
Amit Singh 1Amit Singh 1
HI Sasham,
We use addParameter method of RestRequest class when we want to add the parameter into the URL of the request and addPetemer is basically map which takes Key Value where Key is the parameter and value is the value that we are passing to that param.
In your case use 
request.addParameter('result_type', 'result_type value here');
AND
We use addHeader method of RestRequest Class when we want to add the values into the header and it also takes the parameters in the form of Key Value pair. For example, if we want to add Authorization, Content-Type then we should go for addHeader.
Hope this will help you :)

Regards,
Amit
 
sashamsasham
Hi Amit, Thank you for the details. I disd not undestand why the [] brackets - [&result_type={result}] in the end point 
http://example.com/notif?id={id}&status=resulted[&result_type={result}]
Is it the smae as 
http://example.com/notif?id={id}&status=resulted&result_type={result}
any difference.
One mre question :
When i want to authenticate the request comming to saleforce from external system without using OAuth . 
How to validate the rquest to make sure that it is coming from the source that we expect(Ex- XY company making request to our salesforce)
Isit possible to valdate using 
 String hostname = RestContext.request.Header('Host');. even thoung if they (xyc company) are adding the header for 'Host'