3

I'm trying to use firebase cloud functions to create a proxy to an external json api. But right now I'm just trying to get it all set up.

I wrote this function:

exports.helloWorld = functions.https.onRequest((request, response) => { request.get('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. } }) }); 

I then run the firebase functions emulator and run

curl http://localhost:5000/<project-id>/us-central1/helloWorld 

It returns a message saying the function was triggered, starting execution, but then it just sits there and spins until eventually it times out.

{"error":{"code":500,"status":"INTERNAL","message":"function execution attempt timed out"}} 

I'm not sure what I'm doing wrong.

........

EDIT

This function works perfectly:

exports.helloWorld = functions.https.onRequest((request, response) => { response.send('test'); }) 

2 Answers 2

14

With Cloud Functions, HTTPS type functions are obliged to write a result to the client to indicate that the function is done executing. Until a result is written, the function is assumed to still be running something asynchronous work.

So, when your request is complete, you should be sending some response, even if it's empty. Unfortunately, you've shadowed your primary response object with another one, so you should probably rename one of them:

exports.helloWorld = functions.https.onRequest((request, response) => { request.get('http://www.google.com', function (error, res, body) { if (!error && res.statusCode == 200) { console.log(body) // Print the google web page. } return response.send("") // this terminates the function }) }) 
Sign up to request clarification or add additional context in comments.

5 Comments

I copied and pasted your exact function and it still times out. But I know the firebase setup is correct because if I just send a response with a string it works. See my edit.
I put a return before the send - that should fix it.
I'm sorry for being difficult and I appreciate your help, but it's still timing out. I thought it might be another shadowing issue but I've tried variations of that as well. I've also tried other urls besides google and same thing. Is there a better way to write the function in general?
Look into using the request-promise library to use a promisified version of the request library you're using now. Promises are typically easier to work with than passing callback functions. Also remember that your project has to be on the blaze plan in order to make outgoing http requests.
I'm on blaze. I'll try that. Thanks.
4

HTTPS functions don't complete until you send something on the response. Here's an example that just pipes the content from the proxied request as the output (I had to change the name of the variables to avoid shadowing:

exports.helloWorld = functions.https.onRequest((req, res) => { request.get('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { return res.send(body) // Print the google web page. } return res.send('ERROR: ' + error.message); }) }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.