Tag: 文章

如何从节点服务器发送http post调用?

我尝试使用以下数据在https://api-mean.herokuapp.com/api/contacts上发送通话: { "name": "Test", "email": "test@xxxx.in", "phone": "989898xxxx" } 但没有得到任何回应。 我也试过邮递员工作正常。 邮递员我得到了回应。 我正在使用以下nodejs代码: var postData = querystring.stringify({ "name": "Test", "email": "test@xxxx.in", "phone": "989898xxxx" }); var options = { hostname: 'https://api-mean.herokuapp.com', path: '/api/contacts', method: 'POST', headers: { 'Content-Type': 'application/json' } }; var req = http.request(options, function (res) { var output = ''; res.on('data', function (chunk) { […]

发送一个包含ecoded JSON的POST请求

我正在尝试进行包含JSON编码表单的POST调用。 我为什么这样做? 我没有select,我正在工作的Facebook API,预计接收JSON编码的数据,并在接收JSON时发生错误。 我得到错误types错误TypeError: stringify expects an object做TypeError: stringify expects an object时: var datas = JSON.stringify({ some: "JSON" }); request.post('https://graph.facebook.com/…', { form: datas }, function(error, response, body) { //Fail before the callback call }); 如何避免这一点?

在NodeJS中获取发布数据

我相信这个问题的答案已经过时了,因为这个答案。 我已经阅读了关于npm封装的称为body-parser的解决scheme,但是如果我能find另一种方法,我不想使用它。 我只是简单地想parsing节点中的POST数据。 我有一个像这样的ajax函数: $.ajax { url: '/foo', method: 'POST', data: {foo: "foo", bar: "bar"} } 就像是: app.post('/foo', function(req, res) { var postFoo = req.foo; // How do I do this? });

node.js丢失asynchronous请求中的发布数据

我正在Node.js中做一个简单的表单。 其他一切似乎都在正常工作,但是应该接收发布请求数据的函数永远不会被调用。 以下是相关的代码片段: if (request.method == 'POST') { var body = ''; console.log(request.body); request.on('data', function (chunk) { console.log("got the post request data"); //nothing logged to console body += chunk; }); request.on('end', onRequestEnd(body, response)); } onRequestEnd函数确实被调用,但是稍后我的代码会在参数体中只有一个空string时中断。 关键字“数据”是否正确? 代码从这里的答案修改: 如何提取Node.js中的POST数据? 。 如果需要,我会发布更多。

节点js发送请求原始请求

var req ={ "request": { "header": { "username": "name", "password": "password" }, "body": { "shape":"round" } } }; request.post( {url:'posturl', body: JSON.stringify(req), headers: { "content-type": "application/x-www-form-urlencoded"} }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) } } ); 我想在reqvariables中发送原始请求体。 它正在邮递员上,但在节点js我不能发送原始json作为请求正文发送请求。

从HTML表单发送数据到Node.js服务器

我正在学习Node.js. 我有这个在我的服务器: var http = require("http"); var url = require("url"); http.createServer(function(request, response){ response.writeHead(200, {"Content-Type":"text/plain"}); var params = url.parse(request.url,true).query; console.log(params); var a = params.number1; var b = params.number2; var numA = new Number(a); var numB = new Number(b); var numOutput = new Number(numA + numB).toFixed(0); response.write(numOutput); response.end(); }).listen(10000); 像这样的URL: localhost:10000/?number1=50000&number2=1在我的浏览器上回显50001,所以它的工作原理。 如果不使用Express,我需要通过使用HTML的表单发送这两个参数。 这怎么能实现?

在查询string中发布嵌套的对象 – Node.js

我的代码试图从我的本地Node.js服务器发布数据到Coldfusion API。 我已经设法与API通信,并通过请求头进行身份validation。 然而,我实际上很难通过我的JSON对象,因为我无法得到正确的结构。 API不接受请求模块的JSON选项,所以这是我最简单的select。 该API期待以下内容: { 'source': { 'customer': { 'customerlogin': 'myusername', 'customerpassword': 'mypassword', } } } 我的代码工作,如果我硬编码下面的身体参数(从别人的成功后)到我的文章。 var Jrequest = require('request'); var options = { uri: 'http://myAPI/customerSSO.json', headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': something', 'Timestamp': timestamp}, method: 'POST', body: 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' // Working }; Jrequest(options, function (error, response, body){ res.send(body); }); 如果我通过其他方式发送JSON,例如json.stringify(),则以“源是必需的但未定义的”为由拒绝。 所以我想我的问题是,在node.js中,我怎么把JSON变成看起来像这样的东西 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' 还是我忽略了另一种select? 如果我使用了错误的术语,谢谢你的帮助和道歉。