HTTPNav is a lightweight HTTP/1.1 Go package designed to simplify HTTP request and response handling. It provides custom encoders and decoders to streamline working with HTTP headers and payloads.
Note: HTTPNav does not support HTTPS.
To install HTTPNav, use go get:
go get github.com/udan-jayanith/HTTPNavThen, import it into your Go code:
import HTTPNav "github.com/udan-jayanith/HTTPNav"server contains server data and logic.
HTTPNav.GetServer()GetServer() Function returns a new server.
HandelFunc takes HTTPRequestMethod, requestTarget and a handler. If requests httpMethod and requestTarget matches the handler handler will execute.
Ex:
server.HandleFunc(httpNav.Get, "/", func(response *httpNav.HTTPResponse, request *httpNav.HTTPRequest) {
response.Write([]byte("Hello world. /"))
//Write send a response back to the client.
})StartServer(":8080")StartServer start the server(starts listing to incoming requests).
Parses the HTTP request and store in it. Then pointer to the HTTPRequest is passed to matching HandleFunc()
This return Body as a slice of bytes. If Content-Length header is not included in the header GetBodyAsBytes() returns ContentLengthHeaderNotFound.
bodyBty, err := request.GetBodyAsBytes()var v map[string]any
err := request.GetBodyAsJson(&v)GetBodyAsJson parses the JSON-encoded data and stores the result in the value pointed to by v. If Content-Type is not in the header GetBodyAsJson returns ContentTypeHeaderNotFound. Else if Content-Type != "application/json" GetBodyAsJson returns InvalidContentType. It uses GetBodyAsBytes in underlying layer.
To see other functionalities see HTTP Go Doc or simply see source code.
This project is licensed under the MIT License. See the LICENSE.md file for details.