Here's an example of how to create an HTTP server and read the request body in Go:
main.go517 chars26 lines
In this example, we create an HTTP server on port 8080 by calling http.ListenAndServe
. We then define an HTTP handler function helloHandler
to handle requests to the /hello
path.
In the helloHandler
function, we read the request body using ioutil.ReadAll(r.Body)
, which reads the entire request body into a byte slice. We then print the request body by calling fmt.Fprintf(w, "Hello, %s", requestBody)
and passing the response writer (w
) and the request body (requestBody
) as arguments.
gistlibby LogSnag