To test your Flask app with Postman, follow these steps for each CRUD operation. Ensure your Flask app is running (i.e., app.run(debug=True)
is executed and the server is listening for requests).
You can find and import this configuration in MongoDB_Python_API.json.
Here is an image how the project should look like after loading:
Below is a step by step guide on how to it on your own.
- Set Request Type to
POST
: In Postman, selectPOST
from the dropdown. - Enter URL: Enter
http://localhost:5000/products
. - Set Headers: Set
Content-Type
toapplication/json
. - Enter Request Body: Enter product details in JSON format.
{ "product_id": "123", "name": "Example Product", "price": 29.99 }
- Send Request: Click
Send
and observe the response.
Follow similar steps as above but use the /users
endpoint and a user data JSON.
{
"username": "jonhdoe",
"name": "John Doe",
"email": "[email protected]"
}
- Set Request Type to
GET
. - Enter URL:
http://localhost:5000/products/<product_id>
, replacing<product_id>
with the actual product ID. - Send Request.
Use the /users/<username>
endpoint, replacing <username>
with the actual username.
- Set Request Type to
PUT
. - Enter URL:
http://localhost:5000/products/<product_id>
. - Set Headers and Request Body: Same as in
CREATE
, but with updated data. - Send Request.
Use the /users/<username>
endpoint with updated user data.
- Set Request Type to
DELETE
. - Enter URL:
http://localhost:5000/products/<product_id>
. - Send Request.
Use the /users/<username>
endpoint for deleting a user.
- Ensure that MongoDB is running and accessible.
- Replace placeholders like
<product_id>
and<username>
with actual values present in your database. - After creating an entity (product/users), use the returned ID or username for subsequent Read, Update, or Delete operations.
- For
PUT
requests, ensure your JSON body contains the fields you want to update. - Always check the response in Postman for successful execution or error messages.
Testing with Postman allows you to simulate client requests to your API and observe the behavior of your Flask application, making it an essential tool for development and debugging.
To test your Flask application using curl
, you can perform requests from the command line for each of the CRUD operations.
:warninig: In WSL 2
, localhost
or 127.0.0.1
from within WSL
does not point to your Windows
host by default due to its virtualized network architecture. You need to find the IP address of your Windows
host as seen from WSL
, this can be done in the host terminal with ipconfig
.
Here are examples of curl
commands for each type of request:
curl -X POST http://localhost:5000/products \
-H "Content-Type: application/json" \
-d '{"product_id": "123", "name": "Example Product", "price": 29.99}'
curl -X POST http://localhost:5000/users \
-H "Content-Type: application/json" \
-d '{"username": "johndoe", "email": "[email protected]"}'
curl -X GET http://localhost:5000/products/123
Replace 123
with the actual product_id
.
curl -X GET http://localhost:5000/users/johndoe
Replace johndoe
with the actual username
.
curl -X PUT http://localhost:5000/products/123 \
-H "Content-Type: application/json" \
-d '{"name": "Updated Product Name", "price": 39.99}'
Replace 123
with the product_id
and adjust the JSON data as needed.
curl -X PUT http://localhost:5000/users/johndoe \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]"}'
Replace johndoe
with the username
and adjust the JSON data as needed.
curl -X DELETE http://localhost:5000/products/123
Replace 123
with the product_id
.
curl -X DELETE http://localhost:5000/users/johndoe
Replace johndoe
with the username
.
- Make sure your Flask application is running and listening on the specified port.
- Adjust the
product_id
,username
, and data payload in thecurl
commands to match the actual data in your MongoDB. - The
-H
flag is used to set the request'sContent-Type
header toapplication/json
for POST and PUT requests. - Use the
-X
flag to specify the request method (GET, POST, PUT, DELETE). - The
-d
flag is used to provide the data payload for POST and PUT requests.
curl
is a powerful tool for testing HTTP endpoints and is widely used for its versatility and availability in most operating systems.
Testing a web API through a web browser is straightforward for certain types of requests but limited in capability. In a browser, you can easily perform GET
requests simply by navigating to a URL. However, for other types of requests like POST
, PUT
, and DELETE
, which require a request body or specific headers, a browser isn't natively equipped. For these, you usually need additional tools or extensions.
Here's how you can test your Flask API with a web browser:
For GET
requests, testing is as simple as entering the URL into the browser's address bar. For example:
- To get a product by its ID:
http://localhost:5000/products/<product_id>
- To get a user by username:
http://localhost:5000/users/<username>
Replace <product_id>
and <username>
with actual values from your database.
Browsers don't natively support sending POST
, PUT
, or DELETE
requests directly from the address bar. To test these requests, you have a few options:
-
Browser Extensions: Tools like Postman also offer browser extensions. These extensions allow you to craft requests with different HTTP methods and custom headers and bodies.
-
HTML Forms for POST Requests: You can create a simple HTML form to send
POST
requests. This is somewhat limited, as forms typically send data withapplication/x-www-form-urlencoded
ormultipart/form-data
content types, and handlingJSON
requires additionalJavaScript
.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add Product</title> </head> <body> <h2>Add Product</h2> <form action="http://localhost:5000/products" method="post"> <label for="product_id">Product ID:</label><br> <input type="text" id="product_id" name="product_id"><br> <label for="name">Name:</label><br> <input type="text" id="name" name="name"><br> <label for="price">Price:</label><br> <input type="text" id="price" name="price"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
-
JavaScript: For more advanced testing directly in the browser, you can use JavaScript (for example, via the browser's developer console) to send AJAX requests. This allows you to set custom headers and request bodies, enabling testing of
POST
,PUT
, andDELETE
requests.Example using
fetch
in the browser's console:fetch('http://localhost:5000/products', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ product_id: '123', name: 'Test Product', price: 19.99 }) }) .then(response => response.json()) .then(data => console.log(data));
Testing GET
requests in a web browser is straightforward, but for other request types, consider using tools like Postman, browser extensions, or writing some quick JavaScript for a more comprehensive testing experience. These methods provide the flexibility needed for different HTTP methods and request content types.
← Previous: Running your application with containers | Next: MongoDB Shell (mongosh) Cheat Sheet →