2017-01-03 2 views
1

In POST-Anfragen durch Postman in einer NodeJS API erhalte ich leere Körper ... (Ich bekomme genau das: {}), jedoch von automatisierte Tests funktioniert es perfekt. Eigentlich von Postman das passierte, wenn ich es als "form" oder als "rohe" mit "text" sende, aber wenn ich es als "JSON" in root sende es friert einfach in "loading ..."
Wenn ich suche ich las über diese 2 Zeilen hinzufügen, um Körper im Zusammenhang analysieren sie es für die Tests arbeiten gemacht, aber nicht für Postman:Leere Stelle in POST-Anfragen an eine API in NodeJS von Postman (aber nicht von automatisierten Tests)

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

der gesamte Code ist hier: https://github.com/nemenosfe/TakeMe-Api aber die drei Schlüsseldateien die folgenden (vereinfacht) sind:

app.js:

const express = require('express'); 
const bodyParser = require('body-parser'); 
const app = express(); 
const cors = require('cors'); 
const user = require('./routes/users'); 

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

app.use('/users', user); 

app.use(cors()); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
}); 

app.listen(8888, function() { 
    console.log("Node server running on http://localhost:8888"); 
}); 

module.exports = app; 

Routen/users:

"use strict" 
const express = require('express'), 
     router = express.Router(), 
     /* ... many require and other code probably not relevant for the problem ... */ 

router 
    .post('/', function(req, res, next) { 
    console.log(`conditions: ${(!req.body)} ${(!req.body.uid)} ${(!req.body.provider)} ${JSON.stringify(req.body)}`); 
    // This console.log appears as follows in the console once I make the request by Postman: false true true {} 
    // But it receives what it shoulds with automated tests 

    if (!req.body || !req.body.uid || !req.body.provider) { utilsErrors.handleNoParams(res); } 
     else { 
     /* ... There is a lot of code here but it's not relevant for the problem because it doesn't even reaches this point. */ 
    } 
    }) 

module.exports = router 

Tests/Benutzer:

"use strict" 
let request = require('supertest-as-promised'); 
const api = require('../app'); 
/* ... Another require not relevant for the problem ... */ 
request = request(api); 

describe('Users route', function() { 
    describe.only('POST /users', function() { 
    it("should create a new user when it doesn't exist", function(done) { 
     const params = { 
     'appkey': helperCommon.appkey, 
     'uid': 1, 
     'provider': 'providerTest', 
     'name': 'fakeName' 
     }; 
     request 
     .post('/users') 
     .set('Accept', 'application/json') 
     .send(params) 
     .expect(201) 
     .expect('Content-Type', /application\/json/) 
     .then((res) => { 
      expect(res.body).to.have.property('user'); 
      const userResponse = res.body.user; 
      expect(userResponse).to.have.property('uid', params.uid); 
      expect(userResponse).to.have.property('provider', params.provider); 
      expect(userResponse).to.have.property('name', params.name); 
      /* ... other expectectations that are not important for the problem ... */ 
      done(); 
     }, done) 
    }); 
    }); 

Dank!

Antwort

1

Stellen Sie sicher, senden die POST Anfrage In Postbote als x-www-form-urlenconded

Verwandte Themen