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
mohaaronmohaaron 

Using localhost for REST external/remote site uri

I'm wondering if anyone can tell me why I can't connect to http://localhost from Salesforce to a local rest interface. I've added "http://localhost" as a remote site but I still get an error when connectiong that says the connection failed. The localhost rest uri does work if I use Fiddler or via browser so why doesn't it work from Salesforce?

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

because in oauth the callback url is executed on the client not the server.

All Answers

SuperfellSuperfell

because localhost is relative, so the request originates at a salesforce server, localhost it that salesforce server, not your host. you need to use a publicly accessible IP or host nme.

mohaaronmohaaron

Is this different when using OAuth and the callback url? I have localhost set as the callback url and it works.

SuperfellSuperfell

because in oauth the callback url is executed on the client not the server.

This was selected as the best answer
mohaaronmohaaron

Your saying that when I call localhost from the Salesforce server it's looking at itself instead of my local server?

SuperfellSuperfell

Yes

kennedymankennedyman

I'm not sure about this one. I'm currently developing a solution and am able to use localhost addresses without issue. Salesforce passes the user back to my local app without any problems.

SuperfellSuperfell

It'll only work in the cases where the localhost reference is resolved on the client, like in javascript, or in the oauth flow. it will not work if you use the HTTP classes in apex to make HTTP calls.

sony sonusony sonu
hi
mohaaron... did your problem got solved???
sony sonusony sonu
hi superfell!!!!!!
Louis MonterosaLouis Monterosa
I got this to work by using ngrok io https://ngrok.com/
It exposes your localhost as a http hashed, https hashed.  
I downloaded it to my mac, opened it in my terminal and command cd to it where it was located in my machine.  
The session expires after a set amount of time and you have to grep back into it to produce a new link, when you close your machine or kill the terminal

Start off by:  
Step 1) (on Command Line) cd to the location of the ngrok file you downloaded
Step 2) type  ./ngrok http ####     (the #### sign should be replaced by your localhost port number.)  
In my case it looked like this:
./ngrok http 9750
Step 3) Replace (I used https version - see example below)
I was able to add the prefix to my link by changing localhost:

http://localhost:9750/epiquery1/datahub/...
to
https://4bb7c1d9.ngrok.io/epiquery1/datahub/...

Step 4) Don't forget to add the site under "Remote Site Settings" under Setup in SFDC.  Create a new one, name it, and paste your hashed prefix there.  In this example it would be https://4bb7c1d9.ngrok.io

Hope this helps anyone out in the future when you Google "Salesforce LocalHost"
 
Jennifer ZeisloftJennifer Zeisloft
Thanks @Louis Monterosa for the ngrok suggestion!
For anyone else grappling with this, this thread may also be of help: https://github.com/inconshreveable/ngrok/issues/448
lmonterosa1.3927807890214556E12lmonterosa1.3927807890214556E12
@Jennifer Zeisloft - No problem, I noticed they are trying the ngrok method there too.
Rick Roesler 7Rick Roesler 7
Thanks @Louis Monterosa! This was super-helpful.