InfoWok
⌘K
Beginner

HTTP and Bearer Tokens: A Primer for Python Devs (2026)

An HTTP and bearer token primer for Python devs: methods, status codes, JSON bodies, and how an Authorization Bearer header authenticates an API call.

SK
Sukhveer Kaur
Published June 22, 2026
4 min read
On this page +
HTTP in one picture: request and responseThe methods and status codes you'll actually readThe bearer token: proving who you areMaking an authenticated request in PythonWhy agents and MCP servers lean on thisQuick recapFrequently Asked QuestionsConclusion

The moment an agent tutorial leaves your laptop, the vocabulary shifts: POST this endpoint, send an Authorization: Bearer header, watch for a 401. If HTTP was always something other people’s code handled, that’s where deploy and MCP guides start to blur. This bearer token and HTTP primer covers just enough of the web’s plumbing to make those parts legible.

You don’t need to become a networking expert. You need a handful of ideas — methods, status codes, JSON bodies, and how a token rides along to prove who you are. Get those, and the MCP server and deploy tutorials stop assuming knowledge you were never handed.

🟢 Beginner⏱️ 11 min readStack: Python 3.10+, an HTTP client (httpx)
Before you start
  • You can read a basic Python function and a dictionary — new to that? The Python for AI agents primer covers both
  • That’s it — no prior web or networking background needed
🎯 Key takeaways
  • HTTP is request/response: a client sends a method to a URL, the server replies with a status code and (usually) JSON.
  • The methods that matter are GET (read), POST (send), PUT/PATCH (update), DELETE — plus 200/201/400/401/404/500 status codes.
  • A bearer token proves who you are: send it as Authorization: Bearer <token> on every protected request.
  • Agents and MCP servers run on this — a token on each call is how a server checks the caller before running a tool.

HTTP in one picture: request and response

HTTP is the protocol the web runs on, and the whole model is a conversation: a client sends a request to a URL, and a server sends back a response. Nothing is remembered between requests — each one stands alone, which is why APIs make you send your credentials every time (MDN).

A request carries four things worth knowing: a method (what you want to do), a URL (where), headers (metadata, including auth), and sometimes a body (data you’re sending, usually JSON). The response carries a status code (how it went) and a body (what you got back). That’s the entire shape — everything else is detail on top.

The methods and status codes you’ll actually read

You don’t need all of HTTP, just the common verbs and replies. The methods:

  • GET — read data (“give me this resource”).
  • POST — send data or create something (“here’s a new chat message”).
  • PUT / PATCH — update an existing resource.
  • DELETE — remove one.

And the status codes, grouped by their first digit: 2xx worked (200 OK, 201 Created), 4xx you messed up (400 bad request, 401 unauthorized, 404 not found), 5xx the server messed up (500 server error). Read the first digit and you instantly know who’s at fault — your request or their server. That single habit makes debugging an API call far faster.

📌 JSON is the body format you'll see mostAPI requests and responses almost always carry their data as JSON — the same key/value shape as a Python dictionary. Your HTTP client converts between JSON text and Python objects for you, so a POST body is usually just a dict you hand to the client.

The bearer token: proving who you are

Most useful endpoints aren’t open to the world — you have to prove you’re allowed. The standard way is a bearer token: a secret string you put in the Authorization header on every request. The name is literal — whoever bears the token gets access — so it’s a credential you guard like a password and only send over HTTPS (RFC 6750).

The header has one fixed shape:

css-12neuit
Authorization: Bearer <your-token-here>

The literal word Bearer, a space, then the token. The server reads that header, validates the token, and either serves your request or replies 401 Unauthorized. A 401 almost always means a missing, malformed, or expired token — check the header before anything else.

Making an authenticated request in Python

Here’s the whole thing in code, using httpx (an async-friendly client agents favour). A POST, a JSON body, and a bearer token in the headers:

python
import httpx
headers = {"Authorization": f"Bearer {token}"} # the auth header
payload = {"message": "Hello", "session_id": "abc"} # the JSON body
resp = httpx.post("https://api.example.com/chat", json=payload, headers=headers)
if resp.status_code == 200:
print(resp.json()) # parse the JSON response into a dict
elif resp.status_code == 401:
print("Bad or missing token")

Read it against the four parts: method (post), URL, headers (with the bearer token), and a JSON body. The response gives you a status code to branch on and .json() to turn the reply into a dict. That’s the exact pattern an agent uses to call an MCP server or any authenticated tool — there’s nothing more to it.

⚠️ Tokens belong in headers, over HTTPS — never in the URLDon't put a token in the query string (`?token=...`); URLs get logged, cached, and shared, leaking the secret. Send it in the `Authorization` header, always over `https://`, and load it from an environment variable rather than hardcoding it — the same discipline as an [LLM API key](/llm-api-key-setup-primer/).

Why agents and MCP servers lean on this

Pulling it together: once an agent talks to the outside world, every tool call is an HTTP request, and every protected tool needs a token. A production MCP server validates a bearer token on each request before it runs a single tool — that’s what separates a deployed tool from a public API anyone can hit.

The build-an-MCP-server guide is built entirely on this idea, and the MCP client is the other half: it attaches the token so the agent is allowed in. Read HTTP and bearer tokens fluently and both stop being walls.

Quick recap

The whole primer, in five lines:

  • HTTP is request → response: method, URL, headers, body in; status code and body back.
  • Know GET/POST/PUT/DELETE and 200/201/400/401/404/500 (first digit = who’s at fault).
  • A bearer token proves identity, sent as Authorization: Bearer <token>.
  • In Python, post a JSON body with the token in headers and branch on status_code.
  • Agents and MCP servers validate that token on every protected request.

Frequently Asked Questions

What is a bearer token? A secret string sent in the Authorization header to prove a client may call an API — whoever holds it gets access, so keep it secret and use HTTPS.

What does the header look like? Authorization: Bearer <token> — the word Bearer, a space, the token. A bad one returns 401.

Which methods and codes matter? GET/POST/PUT/PATCH/DELETE, and 200/201/400/401/404/500.

Why do agents use bearer tokens? Their tools are HTTP endpoints; a token per request lets the server authorise the caller before running a tool.

Conclusion

HTTP is just request and response, and a bearer token is just a password that rides in a header to prove you’re allowed in. Learn the handful of methods, read status codes by their first digit, and recognise Authorization: Bearer for what it is, and the deploy and MCP chapters of any agent series stop assuming a background you were never given. It’s the web’s plumbing, demystified to exactly the depth an agent developer needs.

What tripped you up first — status codes, headers, or where the token goes? Tell me in the comments.

🧭 Where to go from here

Frequently asked questions

What is a bearer token? +
A bearer token is a secret string a client sends in the Authorization header to prove it's allowed to call an API. The name means whoever bears (holds) the token gets access, so it must be kept secret and sent over HTTPS. It's the standard way agents and MCP servers authenticate requests.
What does the Authorization Bearer header look like? +
It's a single request header in the form `Authorization: Bearer <token>`. The literal word Bearer, a space, then the token string. The server reads it, validates the token, and either serves the request or returns 401 Unauthorized.
What are the main HTTP methods and status codes to know? +
Methods: GET reads data, POST sends/creates, PUT/PATCH update, DELETE removes. Status codes: 200 OK, 201 Created, 400 bad request, 401 unauthorized, 404 not found, 500 server error. Those cover almost everything you'll meet calling an API.
Why do AI agents and MCP servers use bearer tokens? +
Because their tools are HTTP endpoints that shouldn't be open to the world. A bearer token on every request lets the server check the caller is authorised before running a tool, which is exactly how a production MCP server protects its tools.
Advertisement

References

  1. MDN — An overview of HTTP
  2. MDN — HTTP Authorization header
  3. RFC 6750 — The OAuth 2.0 Bearer Token Usage
  4. HTTPX — A next-generation HTTP client for Python

Tags

#PythonForAI#HTTP#BearerToken#RESTAPI#MCP#AIForDevelopers

Share

Previous Article
LLM API Keys: Set Up OpenAI, Anthropic & Gemini (2026)

One email when something good ships

New guides the day they publish. No digest spam.

InfoWokCode-first AI engineering, in Python.
AboutEditorial standardsContactRSS