Your First GET Request
Make your first API call, parse JSON responses, inspect headers, and validate status codes across all three languages.
Making Your First GET Request
The GET method is the most common HTTP method — it retrieves data from a server without modifying anything. Every API testing journey starts here. We'll hit a public API, inspect the response, and validate what comes back.
What to Validate
When testing a GET endpoint, you should verify:
- Status code — Is it 200 OK?
- Response body — Does the JSON contain the expected fields and values?
- Headers — Is the Content-Type correct? Are there caching headers?
- Response time — Is the API responding within acceptable limits?
Working with JSONPlaceholder
We'll use jsonplaceholder.typicode.com throughout this course — it's a free, public REST API perfect for learning. It provides endpoints for users, posts, comments, albums, photos, and todos.
Parsing JSON Responses
All three libraries make JSON parsing straightforward. The response body comes back as a string, and each library provides methods to convert it into native data structures (objects, maps, dictionaries) for easy field access.
import { strict as assert } from 'assert';
// GET a single resource
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const post = await response.json();
// Validate status code
assert.equal(response.status, 200, 'Status should be 200');
assert.ok(response.ok, 'Response should be OK');
// Validate response body
assert.equal(post.id, 1);
assert.equal(post.userId, 1);
assert.ok(post.title.length > 0, 'Title should not be empty');
assert.ok(post.body.length > 0, 'Body should not be empty');
// Validate headers
assert.ok(
response.headers.get('content-type').includes('application/json'),
'Content-Type should be JSON'
);
console.log('Post title:', post.title);
console.log('All assertions passed!');
// GET a list of resources
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const posts = await response.json();
// Validate we get an array
console.assert(Array.isArray(posts), 'Should return an array');
console.assert(posts.length === 100, 'Should return 100 posts');
// Validate structure of first item
const first = posts[0];
console.assert('id' in first, 'Each post should have an id');
console.assert('title' in first, 'Each post should have a title');
console.assert('body' in first, 'Each post should have a body');
console.assert('userId' in first, 'Each post should have a userId');
// Validate all IDs are unique
const ids = posts.map(p => p.id);
const uniqueIds = new Set(ids);
console.assert(uniqueIds.size === posts.length, 'All IDs should be unique');
console.log(`Fetched ${posts.length} posts successfully`);
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import org.testng.annotations.Test;
public class GetRequestTest {
@Test
public void testGetSinglePost() {
given()
.baseUri("https://jsonplaceholder.typicode.com")
.when()
.get("/posts/1")
.then()
.statusCode(200)
.contentType("application/json")
.body("id", equalTo(1))
.body("userId", equalTo(1))
.body("title", not(emptyString()))
.body("body", not(emptyString()));
}
@Test
public void testGetAllPosts() {
Response response =
given()
.baseUri("https://jsonplaceholder.typicode.com")
.when()
.get("/posts")
.then()
.statusCode(200)
.body("size()", equalTo(100))
.body("[0].id", equalTo(1))
.body("id", everyItem(notNullValue()))
.extract().response();
// Extract values for further validation
int count = response.jsonPath().getList("$").size();
System.out.println("Fetched " + count + " posts");
}
@Test
public void testResponseHeaders() {
given()
.baseUri("https://jsonplaceholder.typicode.com")
.when()
.get("/posts/1")
.then()
.header("Content-Type", containsString("application/json"))
.time(lessThan(5000L)); // Response under 5 seconds
}
}
import requests
BASE_URL = 'https://jsonplaceholder.typicode.com'
def test_get_single_post():
"""Test GET a single resource"""
response = requests.get(f'{BASE_URL}/posts/1')
# Validate status code
assert response.status_code == 200
assert response.ok
# Parse JSON and validate body
post = response.json()
assert post['id'] == 1
assert post['userId'] == 1
assert len(post['title']) > 0, 'Title should not be empty'
assert len(post['body']) > 0, 'Body should not be empty'
# Validate headers
assert 'application/json' in response.headers['Content-Type']
print(f"Post title: {post['title']}")
def test_get_all_posts():
"""Test GET a list of resources"""
response = requests.get(f'{BASE_URL}/posts')
posts = response.json()
assert isinstance(posts, list), 'Should return a list'
assert len(posts) == 100, 'Should return 100 posts'
# Validate structure
first = posts[0]
assert 'id' in first
assert 'title' in first
assert 'body' in first
assert 'userId' in first
# Validate uniqueness
ids = [p['id'] for p in posts]
assert len(set(ids)) == len(posts), 'All IDs should be unique'
print(f"Fetched {len(posts)} posts successfully")
def test_response_time():
"""Validate response time"""
response = requests.get(f'{BASE_URL}/posts/1')
assert response.elapsed.total_seconds() < 5, 'Response should be under 5s'
Written by PV
© 2026 All Rights Reserved