Notes/request_response.go

46 lines
868 B
Go
Raw Normal View History

2023-06-15 07:24:23 +02:00
package main
import (
// Standard
"encoding/json"
2023-06-16 07:11:27 +02:00
"errors"
"io"
2023-06-15 14:12:35 +02:00
"net/http"
2023-06-15 07:24:23 +02:00
)
type Request struct {
ID string
}
2023-06-15 14:12:35 +02:00
func responseError(w http.ResponseWriter, err error) {
res := map[string]interface{}{
"ok": false,
"error": err.Error(),
}
resJSON, _ := json.Marshal(res)
w.Header().Add("Content-Type", "application/json")
w.Write(resJSON)
}
func responseData(w http.ResponseWriter, data interface{}) {
resJSON, _ := json.Marshal(data)
w.Header().Add("Content-Type", "application/json")
w.Write(resJSON)
}
2023-06-16 07:11:27 +02:00
func request(r *http.Request, data interface{}) (err error) {
body, _ := io.ReadAll(r.Body)
defer r.Body.Close()
err = json.Unmarshal(body, data)
return
}
func sessionUUID(r *http.Request) (string, error) {
headers := r.Header["X-Session-Id"]
if len(headers) > 0 {
return headers[0], nil
}
return "", errors.New("Invalid session")
}