Is there a way to get the Sender info in a Rest WebService?
for example :
Part A – Sender sends this JSON Via POST Request :
Http httpProtocol = new Http(); HttpRequest request = new HttpRequest(); String url = 'https://SomeDomainURL'; request.setEndpoint(url); request.setMethod('POST'); request.setHeader('Content-Type','application/json'); String body = '[{"Name":"MyName","LastName":"MyLastName","Age":31}]'; request.setBody(body); HttpResponse response = httpProtocol.send(request);
Part B – The Receiver of the request
//How can I get here some info about the above sender? URL? Domain? etc..
Every info will be good.
The sender is sending the request from Salesforce so I want to get the URL or Domain from the org he sent the request…
I tried to use this :
RestRequest re = RestContext.request; System.debug(re.requestURI); System.debug(re.requestBody); System.debug(re.headers); System.debug(re.remoteAddress); //- This is give me the IP..
But none of this gave me the URLDomain.
The reason I want to check the URL is to whitelist it, instead of using IP.
BTW – When I checked the “remoteAddress” (re.remoteAddress)
I got an Ip number that does not appear over this list: https://help.salesforce.com/articleView?id=000321501&type=1&mode=1
Every help will be grateful.
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Callouts happen from IP and not from URL/Domain. Imagine doing a callout from your personal laptop or mobile device using postman or any other tool, it doesn’t have a domain. It will have IP.
Similarly, the IP of Salesforce callout server keeps on changing. I have seen it change quite frequently during the day, and hence its recommended to whitelist the range and not specific ip’s.
It can happen that the documentation is outdated, in that case, you have to raise a case with SF. They will help you valid IP range for your instance.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0