Question

Functions: Golang raw payload

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?

Show comments

Submit an answer
Answer a question...

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!

Sign In or Sign Up to Answer

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

Matt Welke
DigitalOcean Employee
DigitalOcean Employee badge
February 2, 2023

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

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:

type Event struct {
    Name string `json:"name"`
}

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.

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

You can do this by deploying your function with web: raw in your project.yml file instead of web: 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 the event is map[string]interface{} it is incredibly difficult to understand the true structure of the payload

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 use fmt.Printf etc to print the entire event. Because map[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.

I wasn’t able to deduce this looking about through the documentation

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!

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.