Learn Express routes: curl -X POST command not working

Hi !

I am finishing the “LEARN EXPRESS ROUTES” exercise in the “Learn Express” course.

Learn Express: Create a Server | Codecademy
https://www.codecademy.com/paths/web-development/tracks/javascript-back-end-development/modules/learn-express-create-a-server/lessons/learn-express-routes/exercises/creating-an-expression

I experimented with curl commands to simulate the functions in the programme.

I click the plus (+) sign in the bash command line and execute these command:

Get all expressions
curl --request GET localhost:4001/expressions/
// [{“id”:1,“emoji”:“:grinning:”,“name”:“happy”},{“id”:2,“emoji”:“:sunglasses:”,“name”:“shades”},{“id”:3,“emoji”:“:sleeping:”,“name”:“sleepy”}]

Get expression with id
curl --request GET localhost:4001/expressions/2
// {“id”:2,“emoji”:“:sunglasses:”,“name”:“shades”}

Update expression with id
curl --request PUT localhost:4001/expressions/3?name=also_shades&emoji=%F0%9F%98%80
// {“id”:3,“emoji”:“:sleeping:”,“name”:“also_shades”}

Delete expression with id
curl --request DELETE localhost:4001/expressions/3
curl --request GET localhost:4001/expressions/
// [{“id”:1,“emoji”:“:grinning:”,“name”:“happy”},{“id”:2,“emoji”:“:sunglasses:”,“name”:“happy”}]

But - I cannot get the curl POST request to work.

I tried this:
curl --request POST localhost:4001/expressions?name=another_shades&emoji=%F0%9F%98%80

When I console.log(req.query) it only contain the first part of the query string i.e. “name=another_shades”. “emoji=%F0%9F%98%80” is not part of the req.query.

Do anyone have an idea on what is wrong - what could be a solution?

If you add an apostrophe (') around the url then the cURL POST request works :wink:

curl --request POST ‘localhost:4001/expressions?name=another_shades&emoji=%F0%9F%98%80’

(I still cannot make it work where the POST data is provide by using --data name=another_shades&emoji=%F0%9F%98%80)

By the way a nice way to get the cURL command is to use Google Chrome inspect:

  1. Right-click the browser window where the form you want to submit is
  2. Select Inspect. This will open the DevTools panel.
  3. Select the Network tab
  4. Tick the Preserve log checkbox
  5. Now submit the form
  6. Locate the entry with method POST (look in the Method column).
  7. Right click the line with POST, and select Copy > Copy as cURL
  8. Chrome will copy all the request data in cURL syntax

Voila !