-
Notifications
You must be signed in to change notification settings - Fork 169
Description
Hey there!
Related to #41 and #85, I ran into a lot of confusion trying to sort out what the format should be for the request body to a local background function.
In production a background function has the following for the first parameter:
{ @type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
attributes: null,
data: '<base64 data>' }
and the following for the "context" parameter:
{ eventId: '749799261766789',
timestamp: '2019-09-17T19:56:09.018Z',
eventType: 'providers/cloud.pubsub/eventTypes/topic.publish',
resource: '<pubsub topic>' }
The current docs for using the functions framework for events suggests curl
ing data in a format that results in the parameters not matching the above - the first parameter becomes:
{ message:
{ attributes: {},
data: '<base64 data>',
messageId: '136969346945' },
subscription: 'projects/myproject/subscriptions/mysubscription' }
And the "context" parameter:
{ type: 'true',
specversion: 'true',
source: 'true',
id: 'true' }
Digging into the source code,
functions-framework-nodejs/src/invoker.ts
Lines 431 to 447 in de6ef38
let data = event.data; | |
let context = event.context; | |
if (isBinaryCloudEvent(req)) { | |
// Support CloudEvents in binary content mode, with data being the whole | |
// request body and context attributes retrieved from request headers. | |
data = event; | |
context = getBinaryCloudEventContext(req); | |
} else if (context === undefined) { | |
// Support legacy events and CloudEvents in structured content mode, with | |
// context properties represented as event top-level properties. | |
// Context is everything but data. | |
context = event; | |
// Clear the property before removing field so the data object | |
// is not deleted. | |
context.data = undefined; | |
delete context.data; | |
} |
{ data:
{ data: '<base64 data>`,
attributes: null,
@type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage' },
eventId: 749799261766789 }
(Further evidence this is expected is in this WIP branch for NodeJS samples.)
Using the above POST body seems to result in function parameters that most closely match production, and although it doesn't match the POST body for push messages I'm assuming that's what users should send if they want their function to act closest to what they will see when they deploy to Cloud Functions.
Is this correct?