1

I'm using TypeScript for this and Express, along with Curl for the POST request. Everytime I try to use the POST request using Curl, it always returns undefined.

The function listOfSplashNumbers is a function that takes in an array which contains 9 arrays and a triple.

Something like: "visualisation": '[[5535,2145],[0021,8875],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774, 0854]]'

const app = express();
app.use(express.json());
app.post("/visualize", (req, res) => {
  
  const visualisation = req.body.visualisation;
  console.log(visualisation); //Returns undefined
  const splash = listOfSplashNumbers(visualisation);

});

My Curl command looks like this:

curl http://localhost:5000/visualize \ --request POST \ --header "Content-Type: application/json" \ --data '{"visualisation":[[5535,2145],[0021,8875],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774, 0854]]}'; 
KoyaCho
  • 155
  • 3
  • 12
  • I get "Syntax error: unexpected number in JSON at position x", pointing directly at `0021`. Once I remove the leading zeros, it works fine. Also, do you have those backslashes in the command you're running? Or is it a multiline script? –  Oct 06 '20 at 01:05
  • Changing the leading zeros, made the error different: ```SyntaxError: Unexpected token ' in JSON at position 0``` The backslashes are indeed in the command. – KoyaCho Oct 06 '20 at 08:40
  • Are you running this on Windows? https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl#comment27632717_7173011 –  Oct 06 '20 at 09:47

1 Answers1

1

The issue is with your body json, 0021 and 0854 are causing issue, because number starting with 0 is octal number not integer number either pass them as string or remove trailing 0's

if you try following curl it will work

curl --location --request POST 'http://localhost:5000/visualize' \
--header 'Content-Type: application/json' \
--data-raw '{"visualisation":[[5535,2145],[21,8875],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,8774],[2135,877,854]]}'

You can read more about it Why is JSON invalid if an integer begins with a leading zero?

Adil Liaqat
  • 229
  • 3
  • 11