Welcome to the Linux Foundation Forum!

Ch 06 - Implementing POST, PUT and DELETE with Express (3) using Postman doesn't work

Options

I followed all the instruction on that page. Everything looks good.

I can POST some JSON using

node -e "http.request('http://localhost:3000/bicycle', { method: 'post', headers: {'content-type': 'application/json'}}, (res) => res.setEncoding('utf8').once('data', console.log.bind(null, res.statusCode))).end(JSON.stringify({data: {brand: 'Gazelle', color: 'red'}}))"

It returns 201 {"id":"3"}

and I can GET that using

node -e "http.get('http://localhost:3000/bicycle/3', (res) => res.setEncoding('utf8').once('data', console.log))"

So far so good.

Now I am trying to POST new JSON using POSTMAN by hitting

http://localhost:3000/bicycle

with some JSON in the body

I can get output 201 with response

{
"id" : "4" 
}

Using POSTMAN, I try GET

http://localhost:3000/bicycle/4 

I get 200 OK but without response body. The response body is empty.

Now I try using node

node -e "http.get('http://localhost:3000/bicycle/4', (res) => res.setEncoding('utf8').once('data', console.log))"

I get nothing returned in the console.

My question, it seems like I can only do POST using node command line, but not using Postman. Why is that? Executing POST request using Postman should be the same as using Node cli , right ?

Thank You

Best Answer

  • pnts
    pnts Posts: 33
    edited June 2023 Answer ✓
    Options

    The JSON body in your post request in postman is wrong.
    It should be:

    { "data": { "brand": "Gucci", "color": "Black" } }

    In real life situation there would be validation on that post route and you would not get a 201.

Answers

  • jimmycheung22
    Options

    Adding screenshot.

    Creating new object (POST)

    Getting that object (GET)

    Getting that object using node cli

    Another test:

    Creating another new object and getting it using node cli

    Getting that object (GET) using Postman

    Not sure why I can't GET the new object that was created using Postman , but I am able to get it when it was created using Node cli :s

Categories

Upcoming Training