GET Request
A GET request asks a server to send back a resource, such as a list of products, a user's profile or an HTML page. It is the most common HTTP method: every time you open a URL in the browser, it sends a GET. In JavaScript, you make GET requests with fetch() to load JSON data from an API and display it. GET is meant only for reading, never for changing data on the server.
What a GET request looks like
A GET has a method, a URL, headers and no body. Any extra information, like filters or page numbers, goes in the query string after a ?.
GET /api/products?category=books&page=2 HTTP/1.1
Host: shop.example.com
Accept: application/json
The server replies with a status code, such as 200 OK or 404 Not Found, headers and usually a body containing the data, often in JSON.
Making a GET request with fetch
fetch defaults to GET, so you only need the URL. It returns a promise for a Response object, and reading the body as JSON is a second asynchronous step.
async function getProducts(category, page = 1) {
const params = new URLSearchParams({ category, page });
const response = await fetch(`/api/products?${params}`, {
headers: { Accept: 'application/json' },
});
if (!response.ok) {
throw new Error(`Server responded with ${response.status}`);
}
return response.json();
}
getProducts('books')
.then((products) => {
products.forEach((p) => console.log(p.title, p.price));
})
.catch((err) => console.error('Could not load products:', err.message));
URLSearchParams builds the query string and encodes special characters for you, so a category like "kids & teens" does not break the URL.
Properties that make GET special
- Safe: it should not change anything on the server.
- Idempotent: sending it ten times has the same effect as sending it once.
- Cacheable: browsers, CDNs and proxies can store the response and reuse it.
- Bookmarkable: because everything is in the URL, users can share or bookmark it.
Because of these properties, browsers may prefetch links and crawlers follow them freely. That is why a link like /delete-account?id=5 handled as a GET is dangerous: something could trigger it without the user meaning to.
Mistakes when handling GET responses
The biggest trap with fetch is assuming it rejects on errors. It only rejects when the network fails. A 404 or 500 still resolves, and calling .json() on an HTML error page throws a confusing parse error. Always check response.ok or response.status first.
Second, do not put passwords, tokens or other secrets in the query string. URLs are stored in browser history and server logs. Send tokens in an Authorization header instead.
Third, GET requests should not carry a body. Some servers and tools drop it; fetch refuses to send one and throws an error. If you need to send structured data to filter results, encode it in query parameters, or use POST for complex searches.
Showing loading and error states
A GET takes time, so the interface should show something while waiting and something useful if it fails. Keep three states: loading, data and error. Show a spinner or skeleton while loading, render the data when it arrives, and show a friendly message with a retry button on error.
Takeaway
GET reads data, puts parameters in the URL, has no body and should never change anything. With fetch, check response.ok before parsing, build query strings with URLSearchParams and keep secrets out of the URL.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.