Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js / Express / Mocha / Supertest Rest API - Empty Request Body
    text
    copied!<p>I've looked everywhere I can to find a solution to this. The only thing I've found is an unanswered post. I apologize if I've overlooked something.</p> <p>The problem is that when I try to get the <code>POST</code> values in the <code>/createQuestion</code> API, the body is empty/undefined. I get errors like this <code>Cannot read proprety 'question' of undefined</code> coming from the API.</p> <p>The Express API:</p> <pre><code>app.post("/createQuestion", function(req, res) { var questionType = req.body.question.type; var questionText = req.body.question.text; var questionDuringClass = req.body.question.duringClass; // Do a bunch of stuff res.send(response); }); </code></pre> <p>The test:</p> <pre><code> var should = require('should'); var assert = require('assert'); var request = require('supertest'); var winston = require('winston'); request = request('http://localhost:8080'); describe('Questions', function() { // Test suite before(function(done) { done(); }); it('Should create a freeResponse question', function(done) { // Test case var postData = { "question" : { "type" : "freeResponse", "text" : "This is a test freeResponse question (automated testing)", "duringClass" : "1" } }; request() .post('/createQuestion') .send(postData) .expect(200) .end(function(err, res) { // .end handles the response if (err) { return done(err); } done(); }); }); it('Should delete a freeResponse question', function(done) { // Test case var postData = { "question" : { "type" : "freeResponse", "text" : "This is a test freeResponse question (automated testing)", "duringClass" : "1" } }; request() .post('/deleteQuestion') .send(postData) .expect(200) .end(function(err, res) { // .end handles the response if (err) { return done(err); } done(); }); }); </code></pre> <p>What am I missing? Is the <code>.send()</code> sending the <code>POST</code> data in some different format? Is it not <code>POST</code>ing it to the body of the request?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload