0

So I send a post request using needle with the following object as body:

{ time: 1533725993910,
  rconPort: 52940,
  rconPassword: 'muqrxllv',
  serverPort: 38950,
  unique: 795571399,
  publicIP: 'localhost',
  mods:
   [ { modName: 'clusterio_1.0.0.zip',
       hash: '341feb6c60918c83f7f3a6a14a4a308aef18dda6' },
     { modName: 'clusterio_1.13.1.zip',
       hash: '6124bb9d3896b030fafbef07c971314f4640759f' },
     { modName: 'clusterio_1.13.0.zip',
       hash: '3dde5199f63d87e3e6ea6ef7f522e0df5842dee4' },
     { modName: 'hotpatch-multimod_1.1.4.zip',
       hash: '0a780e23c42452bd51ecc5ce808b65842e77dc85' },
     { modName: 'creative-mode_0.0.1.zip',
       hash: '9af437c10d3f39f1f3ffb57c942c3a51d3786f0f' },
     { modName: 'clusterio_1.12.0.zip',
       hash: '959c82380cad1a6f039de401a98dd70e13ca2224' } ],
  instanceName: 'test',
  playerCount: '0' }

When I console.log(req.body) in the Express handler I get

{ time: '1533722833600',
  rconPort: '52940',
  rconPassword: 'muqrxllv',
  serverPort: '38950',
  unique: '795571399',
  publicIP: 'localhost',
  mods: [ { modName: [Array], hash: [Array] } ],
  instanceName: 'test',
  playerCount: '0',
  mac: '00-FF-8C-0F-21-F0' }

Upon closer inspection it turns out that the "mods" field now looks like this:

"mods":[{
  "modName":[
    "clusterio_1.0.0.zip",
    "clusterio_1.13.0.zip",
    "clusterio_1.13.1.zip",
    "hotpatch-multimod_1.1.4.zip",
    "creative-mode_0.0.1.zip",
    "clusterio_1.12.0.zip"
  ],"hash":[
    "341feb6c60918c83f7f3a6a14a4a308aef18dda6",
    "3dde5199f63d87e3e6ea6ef7f522e0df5842dee4",
    "6124bb9d3896b030fafbef07c971314f4640759f",
    "0a780e23c42452bd51ecc5ce808b65842e77dc85",
    "9af437c10d3f39f1f3ffb57c942c3a51d3786f0f",
    "959c82380cad1a6f039de401a98dd70e13ca2224"
]}]

For logging the data on the server side I use

var app = express();
app.use(bodyParser.json());
app.use((req,res,next) => {
    if(req.originalUrl == "/api/getID"){
        console.log(req.body)
    }
    next()
});

On the client side I do

console.log(payload);
needle.post('localhost:8080/api/getID', payload, needleOptionsWithTokenAuthHeader, function (err, response, body) {

At first I suspected this was an issue with middleware, but as I am logging before all the middleware gets loaded I am not too sure about that anymore.

I have never experienced anything like this, and any help would be appreciated.

Daniel Vestøl
  • 480
  • 7
  • 16

1 Answers1

0

Have you tried to use the function JSON.stringify (doc) ? Formating POST request with tables can be tricky.

To send an object like {a:1, b:{c:2, d:[3, 4]}, e:[5,6]} you have to format your parameters to fit an array associative, so the parameters will be like this:

a=1
b[c]=2
b[d][0]=3
b[d][1]=4
e[0]=5
e[1]=6

To fill a non-associative array you can try the e[]=7 notation.

Chocolord
  • 493
  • 3
  • 12