2016-02-29 11 views
9


ポスト/プットコールを受け入れるノードJSサーバーにリクエストをしようとしています。私がチャイ経由でポストコールで送信しようとしているパラメータは、サーバ(req.body.myparam)には表示されません。
私はポスト要求以下にしようとしたがいない結果で終了している: -
チャイによる投稿依頼

var host = "http://localhost:3000"; 
var path = "/myPath"; 
chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) { 

chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) { 

ノードJSコードは以下の通りです: - コードの上に

app.put ('/mypath', function(req, res){      //Handling post request to create league 
    createDoc (req, res); 
}) 


app.post ('/mypath', function(req, res){      //Handling post request to create league 
    createDoc (req, res); 
}) 

var createDoc = function (req, res) { 
    var myparam = req.body.myparam;         //league id to create new league 
    if (!myparam) { 
     res.status(400).json({error : 'myparam is missing'}); 
     return; 
    }  
}; 

行きますmyparamに行方不明です。

同じことを行う最善の方法を教えてください。
ありがとうございました。

+0

エンドポイントのコードを共有できますか? –

+0

コードを更新しました。何か必要があれば教えてください。 –

+0

「リーグ」はどこにも定義されていませんか? – Derek

答えて

13

あなたの書き方は、chai-httpパッケージを使用したと仮定します。 .field()関数は、chai-httpでは機能しません。別のユーザーがhereを指摘し、githubに問題をオープンしました。

test.js

'use strict'; 
var chai = require('chai'); 
var chaiHttp = require('chai-http'); 

chai.use(chaiHttp); 

describe('Test group', function() { 
    var host = "http://" + process.env.IP + ':' + process.env.PORT; 
    var path = "/myPath"; 

    it('should send parameters to : /path POST', function(done) { 
     chai 
      .request(host) 
      .post(path) 
      // .field('myparam' , 'test') 
      .set('content-type', 'application/x-www-form-urlencoded') 
      .send({myparam: 'test'}) 
      .end(function(error, response, body) { 
       if (error) { 
        done(error); 
       } else { 
        done(); 
       } 
      }); 
    }); 
}); 

:ここ

.set('content-type', 'application/x-www-form-urlencoded') 
.send({myparam: 'test'}) 

が正常にサーバーにパラメータを渡すことを完全なコードは次のとおりです。ここで

は、あなたが書かれている可能性がどのようですserver.js

'use strict'; 
var bodyParser = require("body-parser"), 
    express  = require("express"), 
    app   = express(); 

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

app.put ('/mypath', function(req, res){ //Handling post request to create league 
    createDoc (req, res); 
}); 

app.post ('/mypath', function(req, res){ //Handling post request to create league 
    createDoc (req, res); 
}); 

var createDoc = function (req, res) { 
    console.log(req.body); 
    var myparam = req.body.myparam; //league id to create new league 
    if (!myparam) { 
     res.status(400).json({error : 'myparam is missing'}); 
     return; 
    } 
}; 

app.listen(process.env.PORT, process.env.IP, function(){ 
    console.log("SERVER IS RUNNING"); 
}); 

module.exports = app; 
+0

これは私のために働いた。フォームデータを投稿するときに.field()メソッドを使用すると言うので面白いですが、サーバー上でこのようにbodyParserを使用している場合はうまくいきませんか? –

+0

これにファイルを添付する方法は?私はこれがcode .attach( 'imageField'、fs.readFileSync( 'avatar.png')、 'avatar.png')のコードだと知っていますが、どこにファイルを添付すればいいのですか? – Kannan

3

空のreq.bodyで問題を解決する方法が2つ見つかりました。 application/json Iは.send({foo: 'bar'})としない.field('foo' , 'bar')を使用する両方の場合において

.put('/path/endpoint') 
.set('content-type', 'application/json') 
.send({foo: 'bar'}) 
// .field('foo' , 'bar') 
.end(function(err, res) {} 

// headers received, set by the plugin apparently 
'accept-encoding': 'gzip, deflate', 
'user-agent': 'node-superagent/2.3.0', 
'content-type': 'application/json', 
'content-length': '105', 

として

  1. フォームデータとしてbody

    .put('/path/endpoint') 
    .type('form') 
    .send({foo: 'bar'}) 
    // .field('foo' , 'bar') 
    .end(function(err, res) {} 
    
    // headers received, set by the plugin apparently 
    'accept-encoding': 'gzip, deflate', 
    'user-agent': 'node-superagent/2.3.0', 
    'content-type': 'application/x-www-form-urlencoded', 
    'content-length': '127', 
    
  2. body

    この問題は明らかにchai-httpとは関係ありません。 superagentさんの問題です。 chai-httpはフードの下にsuperagentを使用しています。

    superagentは、機械学習を行い、私たちに推測します。デフォルトでは

    は送信文字列がサポートされているデフォルトの「JSON」と「形」によってただし、Content-Type

    application/x-www-form-urlencodedへたSuperAgent形式は拡張可能です設定されます。ここではそのdocs sayものです。 application/x-www-form-urlencodedとしてデータを送信するには、単に "form"で.type()を呼び出します。デフォルトは "json"です。

    request.post('/user') 
        .type('form') 
        .send({ name: 'tj' }) 
        .send({ pet: 'tobi' }) 
        .end(callback) 
    

    chai-httpの最大の障害は、彼らが適切にプラグインを文書化しなかったことです。インターネット上の回答はすべてchai-httpのGitHubページで検索する必要はありません。

+0

これにファイルを添付する方法は?私はこれがコードであることを知っています.attach( 'imageField'、fs.readFileSync( 'avatar.png')、 'avatar.png') – Kannan