Lesson 09
Using Postman to Test the Sails Blueprint API
Summary
Postman API Request Summary
Overview
In this session, we explored how to use Postman to interact with a RESTful API for managing a "Company" resource on a local server running at port 1337.
Key Steps
-
Default Resource Name:
- The default resource name used in this example is singular ("Company"), but it can be changed to plural if needed.
-
GET Request:
- Performing a
GET /company
request returns a list of all companies. Initially, the list is empty since no records exist.
- Performing a
-
Creating a Company:
- We switched from a
GET
request to aPOST
request to create a new company record. - In the request body, we selected
raw
format and set the data type toJSON
.
- We switched from a
-
JSON Data Format:
- Constructed the JSON object for the company with the following attributes:
"name"
:"App. blah, blah"
"foundingDate"
:"1980-04-11"
(formatted asYYYY-MM-DD
)"employeeCount"
:483
- Important to note: No comma after the last property in JSON.
- Constructed the JSON object for the company with the following attributes:
-
Sending the Request:
- After composing the JSON, we clicked on the "send" button.
- The API provided a response with a
201 Created
status, indicating successful entry in the database.
-
Response Verification:
- The response JSON confirmed that the company record was created, providing:
- An
ID
(e.g.,1
) - Timestamps for creation and last updated (in ISO format)
- An
- The response JSON confirmed that the company record was created, providing:
-
Automatic Properties:
- Notably, we did not need to specify
ID
or timestamp properties in the model; the API handles this by default.
- Notably, we did not need to specify
Next Steps
- In the following lecture, we will explore additional routes and request types to further interact with the API.
Feel free to reach out with any questions about the ISO format or the JSON structure used!
Video Transcript
So let's go back to Postman.
So we got our local host at the part 1337.
So we created a resource called Company.
So by default, Sales uses a singular resource name.
All right?
It can be changed to Plural.
All right?
But let's go with the singular for now.
So if you say about get slash company, that should give you a list of all the companies.
So we got an empty list, as you can see, because we don't have any record, obviously.
Now let's create a company record.
So let's switch from get to a post request.
Now here in the post request, we want to send some data.
So let's go to the body tab, and let's choose no, not form data, but let's choose raw.
And let's switch from text to JSON.
OK?
So we're dealing with JSON, JSON RESTful API.
Let's go JSON all the way.
So for the body of the post request, raw JSON.
So let's make a JSON here, open braces, and let's write, let's create a code.
Create a company.
So a company has a name.
Let's say the name.
I don't know.
App.
App.
Blah, blah.
Note that we're using JSON, so make sure you have the double quotes for the property.
We're using JSON, so we need that.
A company also has a founding date.
So for the format for the date, sales understands the format, the double digit year followed
by a dash, double digit month followed by a dash, double digit day.
So let's say a company has found in the year of 1980.
And a certain company, the month was April, and the day was 11.
So this format is accepted.
The sales will understand this format for the date.
And now a company also has in our example employee count.
And that's just an answer.
Let's say it has, I don't know, 483 employees.
Notice that I'm using JSON, so if you are used to typing commas all the time for property
objects, don't type one here because JSON, this is not valid JSON, so no commas there
for the last property.
So with that in mind, we're going to create this company called Apple, blah, blah, founded
in 1980, April 11th, 483 employees.
So let's click send and see what happens.
So down here, I got my response.
If I scroll down, so this is the response from sales.
It gave me a status 201 created.
And if I try to zoom in here for you to see better, here's the response, JSON response.
So notice it created my record with the information I provided, as well as now it has an ID number
1, because it's now in the database, in this case, local disk database.
And notice that sales by default also adds this created ad and updated ad timestamp
properties.
So notice the format here is always a date followed by time, no matter what.
So this is the format that sales uses.
I think this is called the ISO format.
Correct me if I'm wrong.
Please live in the post, comment if you know about this format.
Anyway, so sales created a record for us, a company record.
It assigned an ID and it started in the database and also created by default these timestamp
properties.
So notice we did not have to specify in the model, if we go back to the API slash model
slash company, we did not specify ID, and we did not specify those created ad and updated
ad timestamps.
So sales does it by default.
So you automatically get those things.
All right.
So going back to postman.
So there you go.
That's the response from sales.
So this is it for this lecture.
Next lecture, we're going to learn more about the other routes and the other requests.
Let's play more around, play more with these requests and see you then.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: