Stephen Handley
Stephen Handley

Reputation: 2455

Are only POST requests available with Google Cloud Functions HTTP Triggers

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?

Upvotes: 2

Views: 12666

Answers (1)

Bret McGowen
Bret McGowen

Reputation: 1360

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!

Upvotes: 7

Related Questions