ProgramGuru

HTTP Fundamentals

Start with HTTP Fundamentals

In this lesson, explore http fundamentals through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of http fundamentals.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

HTTP requests contain a method, URL, headers and sometimes a body. Responses contain a status code, headers and a body. REST APIs commonly use GET to retrieve, POST to create, PUT to replace, PATCH to change and DELETE to remove resources.

Choose status codes that describe the outcome: 200 for success, 201 for created, 400 for invalid input, 404 for missing resources, and 500 for unexpected server failures.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for httpHTTP separates request intent from response outcome.Method + URLStep 1Headers + optional bodyStep 2HandlerStep 3Status + bodyStep 4HTTP separates request intent from response outcome.
HTTP separates request intent from response outcome.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(http.StatusCreated)
		fmt.Fprint(w, "created")
	})
	rec := httptest.NewRecorder()
	handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/tasks", nil))
	fmt.Println("Status:", rec.Code, "Body:", rec.Body.String())
}

Output

Status: 201 Body: created

Trace what the program does

The handler sets a content type before writing status 201 and the body created. The recorder makes the response inspectable.

Avoid common mistakes

  • Write headers before calling WriteHeader or writing the body.
  • Method choice and status codes are part of the API contract.

Try it yourself

Change the handler to return 400 Bad Request with an explanation.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which status usually signals successful resource creation?
Choose one answer. You can try again.

Answer and explanation

201 Created

Correct. The handler sets a content type before writing status 201 and the body created.

What you should remember

You have practiced HTTP Fundamentals in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Creating an HTTP Server →