summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-07-28 18:27:42 +0300
committerPaul Buetow <paul@buetow.org>2024-07-28 18:27:42 +0300
commit8572fca487d124518e46f4fad1919cf98c5ae56a (patch)
treee6f94e230422d3c16043909c768eba3adf3c92d6
parent15e39ae8f63021fd599f6c1f3cfab9bf9c1885b0 (diff)
fix bug where POST turned out to be a HTTP get when there are // in the URI
-rw-r--r--internal/client/tui/submit.go2
-rw-r--r--internal/easyhttp/easyhttp.go5
-rw-r--r--internal/server/handler/handler.go2
-rw-r--r--internal/server/server.go1
4 files changed, 6 insertions, 4 deletions
diff --git a/internal/client/tui/submit.go b/internal/client/tui/submit.go
index 3586244..90143d6 100644
--- a/internal/client/tui/submit.go
+++ b/internal/client/tui/submit.go
@@ -24,7 +24,7 @@ func submitMessage(ctx context.Context, conf client.ClientConfig, filePath strin
servers, err := conf.Servers()
if err == nil {
var entry types.Entry
- err = easyhttp.PostData(ctx, "/submit", conf.APIKey, &entry, servers...)
+ err = easyhttp.PostData(ctx, "submit", conf.APIKey, &entry, servers...)
}
return func() tea.Msg {
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go
index 32a3069..ce3d53b 100644
--- a/internal/easyhttp/easyhttp.go
+++ b/internal/easyhttp/easyhttp.go
@@ -16,7 +16,7 @@ func Get(ctx context.Context, uri, apiKey string) ([]byte, error) {
bytes []byte
)
- req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return bytes, fmt.Errorf("%s: %w", uri, err)
}
@@ -48,11 +48,12 @@ func GetData[T any](ctx context.Context, uri, apiKey string, data *T) error {
}
func Post(ctx context.Context, uri, apiKey string, data []byte) ([]byte, error) {
- req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, bytes.NewBuffer(data))
if err != nil {
return []byte{}, fmt.Errorf("%s: %w", uri, err)
}
+ req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-KEY", apiKey)
client := &http.Client{}
diff --git a/internal/server/handler/handler.go b/internal/server/handler/handler.go
index 41664b1..eabffa3 100644
--- a/internal/server/handler/handler.go
+++ b/internal/server/handler/handler.go
@@ -28,7 +28,7 @@ func New(conf server.ServerConfig) Handler {
func (h Handler) Submit(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
- return fmt.Errorf("expexted POST request")
+ return fmt.Errorf("expected POST request, but got %s", r.Method)
}
bytes, err := io.ReadAll(r.Body)
diff --git a/internal/server/server.go b/internal/server/server.go
index 3c5cd27..5a9e0ce 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -42,6 +42,7 @@ func (serv Server) Handle(name string, handler HandlerFuncWithError) {
}
if err := handler(w, r); err != nil {
+ log.Println(err)
serv.Status.Set(health.Critical, handlerName, err.Error())
return
}