Last active
June 29, 2023 14:27
-
-
Save danielestevez/3473220fe7c90d38b1b1e85e84e6fc75 to your computer and use it in GitHub Desktop.
Golang Webhook listener to receive and parse an event via POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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