- Published on
Using POST request with VS Code's REST Client
- Authors
- Name
- interglobalmedia
- @letsbsocial1
- Link to Using POST request with VS Code's REST Client extension podcast on anchorfm
In my previous podcast entitled Using VS Code's REST Client extension instead of Postman, I discussed using a GET
request with VS Code's REST Client
extension in a production
environment. Now I will discuss using a POST
request in a local
environment.
First I had to add the following to my rest-client.rest
file (I found out that if I added the .rest
extension, it would recognize the http
language by default):
@hostname = localhost
@port = 3000
@host = {{hostname}}:{{port}}
@contentType = application/json
# POST /users
POST http: {{host}}/users HTTP/1.1
Content-Type: {{contentType}}
{
"name": "Michael",
"email": "michael@michael.com",
"age": 23,
"password": "1234567"
}
The http
code in rest-client.rest
is available for viewing in the transcript
post of this podcast
episode on interglobalmedianetwork.com/blog.
This code
differs from the previous request
code in a couple of ways. First of all, it is a POST
request instead of a GET
request. Second of all, the environment is local
. And third of all, I am using environment
variables here.
And this is what I had in my index.js
(my Express server
file):
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
require('./db/mongoose')
const User = require('./models/user')
/*
express will automatically parse incoming json
to an object so we can access it in our request handlers
We can do that using req.body
*/
app.use(express.json())
app.post('/users', (req, res) => {
const user = new User(req.body)
user.save()
.then(() => {
res.send(user)
})
.catch(() => {})
})
})
app.listen(port, () => {
console.log(`Server is running on Port ${port} ...`)
})
When I clicked on "Send Request"
located right above POST
, The following was returned in the Response
:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 119
ETag: W/"77-tTFyEkK9sxYeShiMfIo+M6HknHI"
Date: Wed, 09 Sep 2020 11:13:31 GMT
Connection: close
{
"age": 23,
"_id": "5f58b8dbc3c61a59129690ff",
"name": "Michael",
"email": "michael@michael.com",
"password": "1234567",
"__v": 0
}
And the following was returned in the mongo
shell:
{ "_id" : ObjectId("5f58b8dbc3c61a59129690ff"), "age" : 23, "name" : "Michael", "email" : "michael@michael.com", "password" : "1234567", "__v" : 0 }
And that is it! Well, you get the idea.
Just make sure that the server
is running. Sometimes you may think that it is because you are sure you started running it a little while before, but for whatever reason, you might end up getting inadvertently disconnected (or perhaps you disconnected the server
and forgot!). It is important to check whether the server
is running before clicking
on Send Request
.
In order to make sure you are actually connected to the server
via the REST Client
, make sure that "port"
has been selected with rest-client.rest
. It should look something like this:
The reason why this is possible is because I created some environment
variables including for the port
. And that's what makes it possible for me to connect via localhost
while the server
is running on the same port
as I define in my rest-client.rest
file.
And that is it!
I will be embedding this episode of Plugging in The Holes along with a transcript in the form of a post on interglobalmedianetwork.com
for your hearing and reading pleasure. Bye for now!