diff --git a/README.md b/README.md index b655cd7..f425590 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # Terminal rest client like Postman and Insomnia + diff --git a/internal/gorest/input-handler.go b/internal/gorest/input-handler.go index 964b9a6..7fe6d54 100644 --- a/internal/gorest/input-handler.go +++ b/internal/gorest/input-handler.go @@ -1,6 +1,8 @@ package gorest import ( + "strings" + "github.com/NathanFirmo/gorest/internal/components" "github.com/NathanFirmo/gorest/internal/utils" "github.com/gdamore/tcell/v2" @@ -48,9 +50,13 @@ func (a *App) SetInputHandlers() { a.tview.SetFocus(a.request.NameComponent) } return event - case tcell.KeyEnter: - url := a.request.UrlComponent.GetText() - res, _ := utils.MakeRequest(url) + case tcell.KeyCtrlSpace: + split := strings.Split(a.request.UrlComponent.GetText(), " ") + if len(split) == 1 { + return event + } + + res, _ := utils.MakeRequest(strings.TrimSpace(split[0]), strings.TrimSpace(split[1]), a.request.HeadersComponent.GetText(), a.request.BodyComponent.GetText()) a.response.Component.SetText(string(res)) case tcell.KeyCtrlN: length := a.requestsList.Component.GetItemCount() diff --git a/internal/utils/http.go b/internal/utils/http.go index 179963d..5ae5d65 100644 --- a/internal/utils/http.go +++ b/internal/utils/http.go @@ -2,14 +2,25 @@ package utils import ( "encoding/json" + // "fmt" "io" "net/http" + "strings" ) -func MakeRequest(url string) ([]byte, error) { - client := &http.Client{} +func MakeRequest(method string, url string, rawHeaders string, rawBody string) ([]byte, error) { + req, _ := http.NewRequest(method, url, strings.NewReader(rawBody)) + headers := strings.Split(strings.TrimSpace(rawHeaders), "\n") - res, err := client.Get(url) + for _, h := range headers { + parsedHeader := strings.Split(h, ":") + if len(parsedHeader) == 1 { + continue + } + req.Header.Add(parsedHeader[0], parsedHeader[1]) + } + + res, err := http.DefaultClient.Do(req) if err != nil { return nil, err }