4

This works fine:

var resp = UrlFetchApp.fetch("someremotehost/SomeFile.csv");
resp.getResponseCode();  //returns 200
resp.getContentText();   //returns the data

however, on my local machine, I'm running xampp with SomeFile.csv is located in htdocs/dev but I cannot get it to work on localhost:

var resp = UrlFetchApp.fetch("localhost/dev/SomeFile.csv");
resp.getResponseCode(); //returns 0.0
resp.getContentText()   //returns nothing!

I checked with chrome-extension postman and http://localhost/dev/SomeFile.csv works fine, so why does UrlFetchApp.fetch("http://localhost/dev/SomeFile.csv") not work?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Rx2000
  • 41
  • 1
  • 4
  • Try running the xampp on a different machine and then access with the ipaddress and see if it works or not. – KRR May 06 '15 at 20:43
  • 2
    Previous questions related to this theme: [here](http://stackoverflow.com/questions/17440179/unable-to-connect-google-apps-script-to-mysql-through-localhost), [here](http://stackoverflow.com/questions/13151909/google-apps-script-jdbc-connection-failed), and [here](http://stackoverflow.com/questions/24659033/connection-to-sql-server-2008-r2-db-from-a-google-spread-sheet). I'm sure I've seen more, but can't find them just now. It's a common misunderstanding, that GAS code is running "out in the cloud", and has no access to your machine. – Mogsdad May 07 '15 at 02:56

2 Answers2

6

That wont work because apps script executes code server side (in google servers). The only way to do this is to make an htmlService app and use ajax from the frontend.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Zig, thanks very much for the suggestion! I'm still getting familiar with apps script and UrlFetchApp documentation seems sadly lacking. :-( – Rx2000 May 06 '15 at 23:32
  • 3
    If you got your localhost onto the internet (using DMZ or port mapping on your router, for example), then you'd be able to access it from Apps Script using its *internet address* and port - but "localhost" will never resolve automatically to your machine. – Mogsdad May 07 '15 at 02:58
1

Use Proxy. I had the same issue, where my app was on AWS ec2.

For that, I used nginx as proxy server and it worked.

double-beep
  • 5,031
  • 17
  • 33
  • 41