1
votes

I'm looking to deploy a rest API to Google Cloud Functions, however the deployment docs seem to indicate that it is only possible to use POST requests:

Every HTTP POST request to the function's endpoint (web_trigger.url parameter of the deploy output) will trigger function execution. Result of the function execution will be returned in response body. - https://cloud.google.com/functions/docs/deploying/

Ideally I'd be looking to associate paths with wildcards and across different HTTP methods for example

POST /user
GET  /user/:id
PUT  /user/:id
DEL  /user/:id

with the wildcard values populating some params object in the function context like in Rails, Hapijs, etc.

Wondering if something like the above is possible with Cloud Functions and if not whether it will be in the future?

1
I see examples with GET/PUT/POST at cloud.google.com/functions/docs/writing/http.jarmod
@jarmod yeah but there's no mention of how deployment / route mapping works for that example and only a mention of POST in the deployment section of the docs cloud.google.com/functions/docs/deployingStephen Handley
I updated the docs to remove the misleading POST-only text.Bret McGowen

1 Answers

5
votes

POST-only is a typo in the docs (oops!); I'll get that updated. Google Cloud Function HTTP functions support GET, PUT, POST, DELETE, and OPTIONS.

(See the HTTP functions docs at https://cloud.google.com/functions/docs/writing/http)

If function needs to handle multiple HTTP methods (GET, PUT, POST, and so on), you can simply inspect the method property of the request.

You can inspect the HTTP method via req.method, i.e.

switch (req.method) {
  case 'GET':
    handleGET(req, res);
    break;
  case 'PUT':
    handlePUT(req, res);
    break;
  default:
    res.status(500).send({ error: 'Something blew up!' });
    break;
}

As for the routing/mapping part of your question, currently now there's nothing additional for routing as part of GCF. As always though, stay tuned as we're constantly working on new features!