monobank-firefly3-bot/http.go

34 lines
632 B
Go
Raw Normal View History

2024-03-27 22:28:01 +02:00
package main
import (
"encoding/json"
2024-04-05 17:12:28 +03:00
"errors"
2024-04-10 23:24:16 +03:00
"gitea.stuzer.link/stuzer05/go-monobank"
2024-03-27 22:28:01 +02:00
"io"
"net/http"
)
2024-04-10 16:21:55 +03:00
func readRequestBody(r *http.Request) (monobank.WebHookResponse, error) {
2024-03-27 22:28:01 +02:00
// read body bytes
body, err := io.ReadAll(r.Body)
if err != nil {
2024-04-10 16:21:55 +03:00
return monobank.WebHookResponse{}, err
2024-03-27 22:28:01 +02:00
}
LogString(string(body))
// check empty body
if len(string(body)) == 0 {
2024-04-10 16:21:55 +03:00
return monobank.WebHookResponse{}, errors.New("empty body")
2024-03-27 22:28:01 +02:00
}
// parse body
2024-04-10 16:21:55 +03:00
var transaction monobank.WebHookResponse
2024-03-27 22:28:01 +02:00
err = json.Unmarshal(body, &transaction)
if err != nil {
2024-04-10 16:21:55 +03:00
return monobank.WebHookResponse{}, err
2024-03-27 22:28:01 +02:00
}
return transaction, nil
}