DEV Community

Cover image for What Actually Happens When a Form Hits a Go Server
Valery Odinga
Valery Odinga

Posted on

What Actually Happens When a Form Hits a Go Server

I’ve been learning how to build web servers in Go, and just yesterday, I tried connecting a simple HTML form to my backend.

I expected the flow to be straightforward: user clicks Submit, Go runs my code, output appears. Instead, the page refreshed, my server logs looked fine, but nothing rendered.

That experience made me realize I didn’t fully understand what actually happens when a form hits a Go server.
This article breaks down that process from the browser sending a request, to Go’s net/httphandlers responding focusing on the clear understanding that finally made things click for me.

What Happens When You Click “Submit”

Clicking Submit doesn’t run your Go code. It sends an HTTP request.

The browser packages the form data, chooses a method (usually POST), and sends everything to the server. After that, the browser just waits.

On the Go side, the server is already running and listening. When the request arrives, net/httpmatches the URL to a handler function and calls it. That’s the moment your code actually runs.

Inside the handler, you read the form values, process them, and write a response using http.ResponseWriter. Whatever you write there is what the browser receives and displays.

Why My POST Route Wasn’t Responding

At one point, my server was running fine. The homepage loaded, and my logs showed that the POST route was being hit. Still, nothing showed up in the browser.

The issue wasn’t that the handler wasn’t running it was that I wasn’t sending anything back.

In Go, handling a request and responding to it are two separate steps. If your handler reads the form data but never writes tohttp.ResponseWriter, the browser has nothing to display.

Also the GET and POST aren’t interchangeable. A form that sends a POST request will never hit a GET-only handler, even if the URL looks correct. If the method doesn’t match, your code simply won’t run.

Once I made sure the request method matched and that I was actually writing a response, things finally started to work.

In Go,http.ResponseWriter is how your handler talks back to the browser. Think of it as a pen: whatever you write with it becomes the body of the HTTP response.

For example:

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello, world!")
}
Enter fullscreen mode Exit fullscreen mode

w is the ResponseWriter.

Fprintln(w, ...) writes text into the response.

When the handler finishes, the server sends everything you wrote back to the browser.

If you never write tow, the request still “works” on the server side, but the user sees… nothing. That’s exactly why my POST requests seemed to do nothing: the handler ran, but didn’t write a response.

ResponseWriter also lets you set headers, status codes, or even send files. But at its core, it’s just the channel for your server’s response.

Running a Go server isn’t magic it just listens for requests. Clicking a form’s Submit button sends an HTTP request. Your handler runs when the request matches its URL and method, and http.ResponseWriter is how you send the output back to the browser.

Once I understood this simple flow, everything clicked: method mismatches explain why GET worked but POST didn’t, and writing to ResponseWriter explains why nothing showed up at first.

If you’re starting with Go web servers, remember: request arrives → handler runs → write a response. Master that, and the rest becomes much easier.

Top comments (0)