You can create an HTTP server and read the request body as text in Go using the net/http
package. Here's an example:
main.go544 chars26 lines
In this example, we define an HTTP request handler using http.HandleFunc
that reads the request body using ioutil.ReadAll(r.Body)
, checks for errors, and then prints the body as text using fmt.Fprintf(w, ...)
.
To start the server, we use http.ListenAndServe(":8080", nil)
. This starts an HTTP server on localhost:8080
with the default handler, which is the handler we defined using http.HandleFunc
.
Note that we defer r.Body.Close()
to ensure that the request body is always closed after we're done reading it. This is important to prevent resource leaks.
gistlibby LogSnag