Skip to content

Instantly share code, notes, and snippets.

@danielestevez
Last active June 29, 2023 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielestevez/3473220fe7c90d38b1b1e85e84e6fc75 to your computer and use it in GitHub Desktop.
Save danielestevez/3473220fe7c90d38b1b1e85e84e6fc75 to your computer and use it in GitHub Desktop.
Golang Webhook listener to receive and parse an event via POST
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/webhook", handleWebhookStr)
err := http.ListenAndServe(":8081", nil)
if err != nil {
log.Fatal("Error starting the server: ", err)
}
}
func handleWebhookStr(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var m map[string]interface{}
fmt.Printf("\n Event body was %v", string(body[:]))
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(r.Body)
err := json.Unmarshal(body, &m)
if err != nil {
// handle error unmarshalling
fmt.Printf("Error unmarshalling %v", err)
return
}
fmt.Printf("\n Event received and parsed with Data %v", m)
w.WriteHeader(http.StatusOK)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment