Skip to content
Snippets Groups Projects
Commit fceecc86 authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

crated func CallUrl for webhooks

parent 55edd463
Branches
No related tags found
No related merge requests found
package assignment2
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
var webhooks []WebhookRegistration //Webhook DB
......@@ -38,10 +41,28 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
func ServiceHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case MethodPost:
case http.MethodPost:
fmt.Println("Recive POST request")
for _, v := range webhooks {
go CallUrl(v.URL, "Resonse on registered event in webhook demo: "+v.Event)
}
default:
http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
}
}
func CallUrl(url string, content string) {
fmt.Println("Attempting invocation of url " + url + "...")
res, err := http.Post(url, "string", bytes.NewReader([]byte(content)))
if err != nil {
fmt.Println("Error in HTTP request: " + err.Error())
}
response, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Something is wrong with invocation response: " + err.Error())
}
fmt.Println("Webhook invoked. Received status code " + strconv.Itoa(res.StatusCode) +
" and body: " + string(response))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment