What is REST API? It is a way for two programs to share data over the web by following a common set of rules. One program sends an HTTP request to a URL, and the other sends back a response, usually as JSON.
This page explains the full form, how a request works, methods and status codes, then shows code in JavaScript, Java and Python and how to test an API.
What is REST API full form
The REST API full form is Representational State Transfer Application Programming Interface. Split it into two parts.
- API: a set of rules that lets one program use another. Your weather app uses an API to get the temperature.
- REST: a style for designing web APIs. Roy Fielding described it in his PhD thesis in 2000.
The name makes sense once you break it down. A resource is a thing, such as a student. A representation is a copy of that thing in some format, such as JSON. The server transfers that copy to you.
REST is a style, not a language or a tool. You can build a REST API in Java, Python, JavaScript or many other languages.
What is REST API and how it works
Think of a restaurant. You (the client) read the menu and give an order to the waiter. The kitchen (the server) prepares it. You never enter the kitchen. The menu and the waiter act like the API.
Here is a real exchange. The client asks for student 1:
GET /students/1 HTTP/1.1
Host: api.example.com
Accept: application/json
The server replies:
HTTP/1.1 200 OK
Content-Type: application/json
{"id": 1, "name": "Asha", "city": "Hyderabad"}
| Part | In the example | Meaning |
|---|---|---|
| Method | GET |
What you want to do |
| Path | /students/1 |
Which resource |
| Headers | Accept: application/json |
Extra details |
| Body | None here | Data you send, used with POST and PUT |
| Status code | 200 |
How it went |
| Response body | The JSON | The data you asked for |
Parts of a URL
In https://api.example.com/students/1?fields=name:
httpsis the protocolapi.example.comis the host/students/1is the path, with1as a path parameterfields=nameis a query parameter, used to filter or sort
Pro tip: Name resources with plural nouns and let the method show the action. Use
GET /studentsandDELETE /students/1, not/getStudentsor/deleteStudent?id=1.
REST API methods and status codes
| Method | Job | Example | Safe to repeat? |
|---|---|---|---|
| GET | Read | GET /students |
Yes |
| POST | Create | POST /students |
No, it may create two |
| PUT | Replace fully | PUT /students/1 |
Yes |
| PATCH | Change part | PATCH /students/1 |
Not always |
| DELETE | Remove | DELETE /students/1 |
Yes |
"Safe to repeat" is formally called idempotent. Sending the same request twice leaves the server in the same state as sending it once. The two most used methods are compared in GET vs POST.
Status codes tell you the result:
| Code | Meaning | When |
|---|---|---|
| 200 | OK | A read or update worked |
| 201 | Created | A new item was made |
| 204 | No Content | It worked and there is nothing to send |
| 400 | Bad Request | Your data is wrong |
| 401 | Unauthorized | You are not logged in |
| 403 | Forbidden | You are logged in but not allowed |
| 404 | Not Found | No such item |
| 409 | Conflict | For example, the email already exists |
| 429 | Too Many Requests | You passed the limit |
| 500 | Internal Server Error | The server failed |
Codes in the 400s mean the request has a problem. Codes in the 500s mean the server has one.
Common mistake: Returning 200 for everything, with
"error": truein the body. Programs check the status code first, so use the right one.
The rules that make an API RESTful
Fielding listed six rules, called constraints.
- Client and server are separate. Each can change without breaking the other.
- Stateless. Each request carries all the details it needs. The server does not remember your last request.
- Cacheable. Responses say whether they may be stored and reused.
- Uniform interface. Resources have clear URLs and standard methods.
- Layered system. A gateway or cache may sit in between, and the client need not know.
- Code on demand. The server may send code to run. This one is optional.
Stateless does not mean the server has no database. It means the server does not depend on a saved conversation between requests. That is why you send a token each time.
Many APIs called REST follow only some of these rules. People often say "REST" for any JSON API over HTTP.
Build a small REST API with examples
This one runs on Node.js with no extra packages. Save it as server.js.
const http = require("http");
const students = [{ id: 1, name: "Asha" }];
const send = (res, code, data) => {
res.writeHead(code, { "Content-Type": "application/json" });
res.end(JSON.stringify(data));
};
const server = http.createServer((req, res) => {
const match = req.url.match(/^[/]students[/]([0-9]+)$/);
if (req.method === "GET" && req.url === "/students") {
return send(res, 200, students);
}
if (req.method === "GET" && match) {
const found = students.find((s) => s.id === Number(match[1]));
if (found) return send(res, 200, found);
}
send(res, 404, { error: "Not found" });
});
server.listen(3000);
Start it with node server.js, then open http://localhost:3000/students in a browser. The results are:
| Request | Status | Body |
|---|---|---|
GET /students |
200 | [{"id":1,"name":"Asha"}] |
GET /students/1 |
200 | {"id":1,"name":"Asha"} |
GET /students/9 |
404 | {"error":"Not found"} |
Real projects use a framework for routing, checks and security. The Node JS tutorial is a good next step.
What is REST API in Java
In Java, most teams build REST APIs with Spring Boot. A class marked @RestController turns method results into JSON.
@RestController
@RequestMapping("/students")
public class StudentController {
private final Map<Integer, String> data = Map.of(1, "Asha");
@GetMapping("/{id}")
public ResponseEntity<String> one(@PathVariable("id") int id) {
String name = data.get(id);
return name == null
? ResponseEntity.notFound().build()
: ResponseEntity.ok(name);
}
}
It needs a Spring Boot web project and imports, so it will not run alone. The method sends 200 with the name, or 404 if the ID is unknown. To build a complete one with a service and a database, follow REST API with Spring Boot.
What is REST API in Python
In Python, common choices are FastAPI, Flask and Django REST framework. Here is the same idea in FastAPI:
from fastapi import FastAPI, HTTPException
app = FastAPI()
students = {1: {"id": 1, "name": "Asha"}}
@app.get("/students/{student_id}")
def get_student(student_id: int):
if student_id not in students:
raise HTTPException(status_code=404, detail="Not found")
return students[student_id]
You need to install FastAPI and a server such as Uvicorn. FastAPI reads the int type and rejects /students/abc for you, with status 422. It turns the Python dictionary into JSON.
To call an API from Python, many people use the requests package:
import requests
r = requests.get("http://localhost:3000/students/1", timeout=5)
r.raise_for_status()
print(r.json()["name"])
With the Node server above running, this should print Asha. Always set a timeout so your program does not wait forever.
What is REST API testing
REST API testing means sending requests and checking that each response is correct. You test the API directly, with no screen.
Check five things:
- Status code: is it 200, 201 or 404 as expected?
- Body: are the fields, types and values right?
- Headers: is the content type JSON?
- Errors: what happens with missing or wrong data?
- Security: can one user read another user's data?
Test for GET /students/{id} |
Expect |
|---|---|
| ID 1, which exists | 200 and name Asha |
| ID 9, which does not exist | 404 |
ID abc |
400, 404 or 422, based on the API rules |
| No login token on a private API | 401 |
Start by hand with Postman or with curl:
curl -i http://localhost:3000/students/1
The -i flag also shows the status line and headers. Later, automate the tests with REST Assured in Java, pytest with requests in Python or Supertest in Node.js.
Interview note: "How would you test a login API?" Cover a right password, a wrong password, empty fields, a locked user, too many tries, and that the password never appears in the response.
REST compared with SOAP and GraphQL
| Point | REST | SOAP | GraphQL |
|---|---|---|---|
| What it is | A style | A strict protocol | A query language |
| Data format | Usually JSON | XML | JSON response |
| URLs | One per resource | Usually one service URL | Usually one URL |
| Who picks the fields | Mostly the server | The server | The client |
| Common use | Web and mobile apps | Some older bank and business systems | Screens that need data from many places |
REST is the best first choice for a learner because the ideas carry over to the others.
Keep going with REST API interview questions. To know who may call an API and what they may do, read authentication vs authorization. To build APIs with a trainer, see the Java full stack course at CodeBegun.
Frequently Asked Questions
What is a REST API in simple words?
What is the difference between API and REST API?
Is REST API the same as HTTP?
What is the difference between PUT and PATCH?
Which tool should a beginner use to test a REST API?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

