HTTP Basics
By the Syncfusion DevTutorials team · Last updated
Read every line of a real HTTP request and response: the method, path, query string, headers and JSON body, and why a missing Content-Type breaks a request.
HTTP (HyperText Transfer Protocol) is the set of rules that clients and servers use to communicate with each other over a network. Whenever an app requests data from a server or sends data to a server, it uses HTTP messages. Every API call is simply an HTTP request sent by a client and an HTTP response returned by a server. Learning HTTP helps you understand exactly how APIs work and makes it much easier to find and fix problems when something goes wrong.
Watch an App Fail
The Brew & Bytes app sends an order. The server rejects it. The app only shows "Something went wrong", so nobody knows the real reason. The example below sends the same order three different times. Press Run and look carefully at each response.
Program.cs — POST an order three ways
- The first request succeeds with
201 Created. - The second request returns
415 Unsupported Media Type. The request does not include aContent-Typeheader, so the server does not know that the body contains JSON. - The third request returns
400 Bad Request. The JSON data is incomplete and cannot be read correctly.
In simple words: the error information was already in the request and response messages. When you learn how to read those messages, you can quickly understand what went wrong instead of guessing.
TL;DR
- HTTP (HyperText Transfer Protocol) is the language used by clients and servers to communicate.
- A request contains a start line, headers, a blank line, and an optional body.
- A response contains a status line, headers, a blank line, and an optional body.
- A header provides extra information about the request or response.
- JSON is the most common data format used in API bodies.
- When something fails, read the request and response before changing any code.
A Real-World Example
Think about sending a parcel. The label tells where the parcel should go. Stickers provide extra instructions such as "Fragile" or "Handle with care". Inside the parcel is the actual item being sent. An HTTP message works in a similar way.
| Parcel | HTTP Message |
|---|---|
| Address label | Start line: POST /orders |
| Instruction stickers | Headers such as Content-Type: application/json |
| Space between the label and contents | Blank line |
| The item inside | Body such as {"item":"Latte","quantity":2} |
If a parcel does not have the correct handling instructions, the courier may not know how to treat it. Similarly, if a request does not include Content-Type, the server may not know how to process the body correctly.
Every HTTP message has four parts: a start line, headers, a blank line, and a body.
What You Will Learn
In this lesson, you will learn:
- The four parts of every HTTP request and response.
- What methods, paths, and query strings are.
- What headers do and why they are important.
- How JSON is sent in the body of a request or response.
- How to read an HTTP response manually.
In this course you read messages as plain text. Real HTTP/1.1 messages on the network look exactly like this. Newer versions (HTTP/2 and HTTP/3) pack the same parts into a faster binary format, but the parts and their meaning do not change.
The Request
Start Line, Headers, and Body
Every HTTP request starts with a single line called the start line.
The start line contains:
- The method
- The path
The method tells the server what action the client wants to perform.
For example:
GETmeans "send me data".POSTmeans "create something new".
The path tells the server which resource the client wants.
Examples:
/menu
/orders
The query string comes after a ? and provides extra information.
Example:
/orders?table=7
After the start line come the headers.
Each header contains:
Name: Value
Then there is a blank line. After the blank line comes the body, if the request includes one. The next example shows a server that simply returns information about the request it received. Press Run. Then try changing the path or adding a custom header such as:
X-Shop: MG Road
Program.cs — echo what the server received
The response shows:
- The request method
- The path
- The query string
- The headers
X-App-Version is a custom header used by the application to tell the server which app version is making the request.
Common Headers
| Header | Purpose |
|---|---|
Accept | Tells the server which response format the client wants |
Content-Type | Tells the server what format the body uses |
User-Agent | Identifies the application making the request |
Authorization | Identifies the caller and permissions |
The Query String
The method says what to do, the path says which resource, the query string adds options, and headers and body send additional information.
Small pieces of information are usually sent in the query string. For example, a client may request only menu items priced at ₹150 or less.
Program.cs — filter with the query string
The first request uses:
?maxPrice=150
Only the matching menu items are returned. The second request has no query string, so all menu items are returned. In C#, the parameter int? maxPrice is automatically filled from the query string. The ? indicates that the value is optional. In Hono, c.req.query("maxPrice") reads the same value.
Header names do not care about capital letters. Content-Type, content-type and CONTENT-TYPE are the same header. Paths are different: Hono treats /menu and /Menu as two addresses, while ASP.NET Core treats them as one. Use lowercase paths and you never have to think about it.
The Response
Status Line, Headers, and Body
An HTTP response also follows a standard structure. It begins with a status line.
Example:
HTTP/1.1 200 OK
The status line contains:
- HTTP version
- Status code
- Status message
The status code tells the client what happened. After the status line come:
- Headers
- A blank line
- The body
Servers can also add custom headers. The example below adds caching information and a custom header.
Program.cs — add your own response headers
Notice these headers:
Cache-ControlX-ShopContent-Type
Content-Type is added automatically because the response body contains JSON. Headers beginning with X- are commonly used for custom information.
Read a Response by Hand
Before any library processes an HTTP response, it is simply text. The following example breaks a raw HTTP response into its individual parts.
ResponseReader.cs — split a raw response into parts
The response contains:
- A status line.
- One or more headers.
- A blank line.
- A body.
When you understand these parts, debugging API problems becomes much easier.
JSON in the Body
JSON (JavaScript Object Notation) is the most common format used to send data in APIs.
JSON uses:
{ }for an object.[ ]for a list.- Double quotes for text.
- Numbers without quotes.
Look again at the example near the top of this lesson.
The same JSON order was sent three times:
- With
Content-Type: application/jsonand valid JSON, the order was created successfully. - Without
Content-Type, the server returned415 Unsupported Media Type. - With incomplete JSON, the server returned
400 Bad Request.
In ASP.NET Core, these checks happen automatically before your endpoint code runs. In Hono, you typically add these checks yourself.
Always send Content-Type: application/json with a JSON body. A missing or wrong Content-Type is one of the most common reasons a request fails. The error is 415 Unsupported Media Type, and it has nothing to do with your data.
Where You See This
- Browser Developer Tools: Open the Network tab to view requests and responses.
- HTTP Request Files: Visual Studio, Rider, and VS Code can send requests from
.httpfiles. - Command Line Tools: Tools such as
curlallow you to send requests and inspect raw HTTP responses.
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
415 Unsupported Media Type | The request has no Content-Type, or the wrong one | Add Content-Type: application/json |
400 Bad Request on a JSON body | The JSON is broken: a missing quote, comma or bracket | Check the JSON. Paste it into a JSON checker |
| The server ignores your filter | There is a typo in the query string, such as ?maxprize=150 | Compare the name with the documentation of the API |
| The body arrives empty | There is no blank line between the headers and the body (when you write requests by hand) | Leave one empty line after the last header |
Exercises
What to Remember
- Every HTTP message has four parts: a start line, headers, a blank line, and a body.
- A request starts with a method and a path. A response starts with a status code.
- Headers carry extra information.
Content-Typesays what the body is. - Small details go in the query string, after
?. - When something fails, read the raw messages first.
Next: HTTP Methods. You will learn what GET, POST, PUT, PATCH and DELETE mean, and how to make a retry safe.