Postman Requests Exercise
Understanding the Problem
We need to interact with a local server using Postman to perform several HTTP operations:
- Create a new post
- Add two comments to that post
- Edit the post
- Delete one of the comments
We'll be working with a server running on localhost:5000 that provides various endpoints for managing posts and comments.
Devising a Plan
- Start the local server using npm start
- Create a new post using POST /posts
- Verify post creation using GET /posts
- Add two comments using POST /posts/:postId/comments
- Verify comments using GET /posts/:postId
- Edit the post using POST /posts/:postId
- Delete one comment using POST /comments/:commentId/delete
- Verify final state using GET endpoints
Carrying Out the Plan
Step 1: Server Setup
# In terminal:
cd server
npm install
npm start
Step 2: Create New Post
Request:
Method: POST
URL: http://localhost:5000/posts
Headers: Content-Type: application/x-www-form-urlencoded
Body:
title: My First Post
description: This is a test post
Step 3: Add Comments
Request (repeat twice with different text):
Method: POST
URL: http://localhost:5000/posts/1/comments
Headers: Content-Type: application/x-www-form-urlencoded
Body:
text: This is comment 1
Step 4: Edit Post
Method: POST
URL: http://localhost:5000/posts/1
Headers: Content-Type: application/x-www-form-urlencoded
Body:
title: Updated Post Title
description: Updated post description
Step 5: Delete Comment
Method: POST
URL: http://localhost:5000/comments/1/delete
Headers: Content-Type: application/x-www-form-urlencoded
Looking Back & Real-World Applications
Understanding HTTP Methods
Think of HTTP methods like different types of interactions in a library:
- GET: Like looking up a book (reading)
- POST: Like adding a new book to the collection (creating)
- PUT/PATCH: Like updating a book's information (updating)
- DELETE: Like removing a book from the library (deleting)
Real-World Examples
These operations are similar to:
- Creating a social media post
- Commenting on a YouTube video
- Editing your profile information
- Deleting a tweet
Common Pitfalls
- Forgetting to set the Content-Type header
- Using wrong HTTP methods
- Not verifying operations with GET requests
- Forgetting to include required fields in the body
Testing Tips
- Always verify operations with GET requests
- Test with invalid data to understand error handling
- Keep track of created resource IDs
- Check response status codes and messages
Extended Learning
To deepen your understanding, try:
- Creating multiple posts and managing their relationships
- Testing edge cases with empty or invalid data
- Exploring different response formats (JSON vs HTML)
- Understanding RESTful API conventions