Skip to content

Commit

Permalink
feat: handle headers and body
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFirmo committed Jan 20, 2024
1 parent c5472f3 commit 2596006
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Terminal rest client like Postman and Insomnia


12 changes: 9 additions & 3 deletions internal/gorest/input-handler.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 14 additions & 3 deletions internal/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2596006

Please sign in to comment.