Hi there,
I have been experimenting with Functions using the golang runtime and I was wondering if there was a way to either get the raw payload from the event in the form of a http.Request
struct or if it was possible to configure the application in a way to have a POST body encoded as a json string inside the event payload?
As the event is map[string]interface{}
it is incredibly difficult to understand the true structure of the payload and I wasn’t able to deduce this looking about through the documentation.
Has anyone else figured out how to do this yet in Golang?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
I had a similar issue so I created a package to help handle it, its not perfect but it helps a bit
https://gitlab.com/tin-roof/meerchat-aac/request
The way our Go runtime works, you can have the event payload parsed into any struct that implements JSON deserialization. The easiest way to accomplish this is to provide a struct where you define a field for every piece of data you want to process and use the “json” tag on the fields. Example:
You would use the struct as the first parameter of the function you define.
Using structs you don’t control like
http.Request
would generally not work well because you wouldn’t be in control of how they’re defined and therefore you wouldn’t be in control of how the Functions system deserializes the event payload into them.You can do this by deploying your function with
web: raw
in yourproject.yml
file instead ofweb: true
. See the answer at https://www.digitalocean.com/community/questions/in-functions-i-handle-a-payload-how-do-i-stop-digitalocean-storing-their-data-or-access-the-raw-payload?comment=193151 for an example of doing this.As you note, you can use the type
map[string]interface{}
instead of a JSON-deserializable struct for the first parameter of the function you define, so if you want to get a sense of all the data available, you can deploy a function like this and usefmt.Printf
etc to print the entire event. Becausemap[string]interface{}
is JSON-deserializable, in a way which results in all of the available being deserialized into it, you will see all of the event data if you do this.Thanks for providing this feedback. We’re in the process of updating our documentation which should make it easier to learn about the data available in the event payloads passed into your function parameters. Be sure to keep an eye out for an update to it!