Skip to content

Add example on how to send an event as a client in cloud_run_event #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
origliante opened this issue Aug 24, 2020 · 4 comments
Open
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@origliante
Copy link

As in the title, I searched like a crazy and found nothing, an example would be great

@di
Copy link
Member

di commented Aug 24, 2020

Thanks for the issue. There are some examples in #23 but ultimately this is blocked on that issue -- the "correct" way would be with the emulator.

@di di added the documentation Improvements or additions to documentation label Aug 24, 2020
@origliante
Copy link
Author

You're welcome, thanks for pointing me there, I'm leaving a quick example here for people stumbling upon the issue
If you are running:

functions-framework --signature-type=event

This is the way to send an event to the server:
$ curl -d '{"data": {"hi": "there"}}' -X POST -H "Content-Type: application/json" http://localhost:8080

@MadJlzz
Copy link

MadJlzz commented Feb 9, 2021

Indeed, it's missing documentation on how to call a function when the type is not http.
It's quite straightforward though. After some experiments, with a payload that looks like (for a function of type event):

{
    "eventId": "test",
    "timestamp": "2018-04-09T07:56:12.975Z",
    "eventType": "google.pubsub.topic.publish",
    "resource": "my-app",
    "data": {
        "data": "dGhpcyBpcyBkYXRhIG5vb2I=",
        "attributes": {
            "app": "my-app",
            "version": "1.0.0"
        }
  }
}

it returns

{'data': 'dGhpcyBpcyBkYXRhIG5vb2I=', 'attributes': {'app': 'my-app', 'version': '1.0.0'}}
{event_id: test, timestamp: 2018-04-09T07:56:12.975Z, event_type: google.pubsub.topic.publish, resource: my-app}

by doing simply print(event) and print(context)

with a function runned with:
functions-framework --target=<name_of_function_to_run> --signature-type=event

Please note that I may have found mismatch with the documentation here.

For example, you have to pass eventId and not event_id for the param to be correctly passed.

Hope this helps and available for PR if needed 😃

@grayside
Copy link
Contributor

Perhaps linking to https://cloud.google.com/functions/docs/running/calling#background_functions would be useful?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants