通过柴发布请求

编程入门 行业动态 更新时间:2024-10-25 05:18:10
本文介绍了通过柴发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试向我的节点JS服务器发出请求,该服务器接受发布/发布调用.我尝试通过chai通过邮寄发送的参数在服务器(req.body.myparam)上不可见. 我已经尝试过以下发布请求,但最终没有结果:-

I am trying to make a request to my node JS server which accepts post/put call. The parameters I am trying to send with post call via chai is not visible on server (req.body.myparam). I have tried with below post request but ended with not results:-

var host = "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.

Above code goes to myparam is missing.

请让我知道执行此操作的最佳方法是什么. 预先感谢.

Please let me know what is the best way to do the same. Thanks in Advance.

推荐答案

按照您的编写方式,我假设您使用的是 chai-http 软件包. .field()函数在 chai-http 中不起作用.另一个用户在此处指出了这一点,并在 github .

The way you have written, I assume that you used chai-http package. The .field() function does not work in chai-http. Another user pointed it out here and opened an issue on github.

这是你应该怎么写的:

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

这是成功成功将参数传递给服务器的完整代码:

Here is the full code that successfully passes parameters to the server:

test.js

'use strict'; var chai = require('chai'); var chaiHttp = require('chai-http'); chai.use(chaiHttp); describe('Test group', function() { var host = "" + 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(); } }); }); });

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;

更多推荐

通过柴发布请求

本文发布于:2023-10-10 22:33:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!