I am working on a simple texteditor that saves and loads text files through an Node/ExpressJS server. Loading is fine, but saving doesn't work yet due to me not being able to transmit the data to the server-app correctly.
I send the data via XMLHttpRequest to the server in a POST request, which works fine according to the network-profiler in dev-tools, the 'handler_save' function is called, but no parameters are received.
What am I doing wrong? (here is a snippet of the server code, altered for demonstration:)
express = require('express')();
function init_save_load(){
var bodyParser = require('body-parser');
express.use(bodyParser.urlencoded({ extended: true }));
express.use('/save', handler_save );
express.use('/load', handler_load );
}
...
function handler_save(req, res){
console.log(req.body); // "{name:post.txt,data:testing}"
}
make sure you are parsing the request body so it can work
var bodyParser = require('body-parser');
app.use(bodyParser());
bodyParser is a part of "Connect", a set of middlewares for node.js. Here's the real docs and source from Connect: http://www.senchalabs.org/connect/bodyParser.html
finally console log the req.body and see what is in there
console.log(req.body)
Not only do you need to use a body parsing middleware as Abdul mentioned, but your request needs to have the correct Content-Type. Currently you are sending Content-Type: text/plain, but it should be Content-Type: application/x-www-form-urlencoded for simple forms or Content-Type: multipart/form-data for forms containing files.
Related
In PHP I can fetch incoming JSON from let's say an AJAX request by calling file_get_contents('php://input'), I'm now rebuilding my API in NodeJS (TypeScript) and am looking for an equivalent to fetch incoming JSON.
Various web searches did not deliver what I was looing for, so I hope to find help here.
You can try to call the correct URL by using Axios. Following this link https://www.npmjs.com/package/axios for more.
You can use bodyparser for express and receive data via post.
let app = express();
const bodyParser = require("body-parser");
app.listen(port);
console.log('API started port' + port);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/path', "maybe cors()", function(req, res){
let data = req.body.data //data is the json you get
}
Thats the way i fetch json that gets send to the server. Hope you can make something with it.
I have a node.js + Express application. It has a webhook that I have provided to a third party service. The service sends a POST request to my webhook with JSON body which looks something like this:
{"split_info" : "null", "customerName" : "Merchant Name",
"additionalCharges" : "null", "paymentMode":"CC",
"hash":"a31ff1b91fd9b8ae9c82f38b02348d21fsdfd86cc828ac9a0acf82050996372cc656de3db0fe3bf9af52b73a182a77787241f3e19ec893391607301b03e70db8",
"status" : "Release Payment", "paymentId" : "551731" ,
"productInfo":"productInfo", "customerEmail":"test#gmail.com",
"customerPhone":"9876543212", "merchantTransactionId":"jnn",
"amount":"100.0", "udf2":"null", "notificationId" :"4", "udf1":"null",
"udf5":"null", "udf4":"null", "udf3":"null","error_Message":"No
Error"}
I am using body-parser module to read POST data. However when I do req.body it gives [object Object], if I do JSON.stringify(req.body), it gives {} i.e. empty. If I try to access the keys in the response like req.body.paymentMode then it gives undefined.
Here is my router code for the webhook: mywebhook.js
var express = require('express');
var router = express.Router();
router.post('/success', function(req, res){
//this is where I need to strip the JSON request
//req.body or JSON.stringify(req.body) or anything that works
//if everything is okay then I send
res.sendStatus(200);
});
module.exports = router;
My app.js looks like this:
var express = require('express');
var exphbs = require('express-handlebars');
var router = express.Router();
var bodyParser = require('body-parser');
var mywebhook = require('./routes/mywebhook');
var app = express();
.
.
.
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use('/callwebhook', mywebhook);
.
.
.
so on
Pretty sure I am missing something or doing something wrong, but I am not able to figure it out.
Thanks.
I finally found what was going on.
The way the body-parser works is that it will only try to parse a request in which they understand the Content-Type. This is mainly so you can stack them (app.use multiple parser types without conflict) and also because you typically don't want to parse a request that's going to fail (Content-Type: text/html is unlikely to ever get through a JSON.parse, for example).
I ended up getting sent */*; charset=UTF-8 which is not even a valid Content-Type header value period. The body-parser module refused to accept it, since that is gibberish. This module does allow you to setup a function that lets you put any custom logic you want to perform the filtering.
I had to put the body parser in my router code just for this webhook case.
var bodyParser = require('body-parser');
var customParser = bodyParser.json({type: function(req) {
return req.headers['content-type'] === '*/*; charset=UTF-8';
}});
router.post('/success', customParser, function(req, res){
console.log(JSON.stringify(req.body));
});
#svens thank you for your help.
Have looked for similar questions and can't find anything on this, so apologies if it already exists.
I'm pretty new to front-end work so I've got a fairly simple personal project on the go. I've got a pretty basic node/express setup and am trying to initiate a GET request to the /next-user URL
the GET is set up in the server as:
app.get('/next-user', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
console.log(req.body)
})
I know a common issue with this is that the body-parser module is not setup, so here is my setup code:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
and the request is setup like;
var data = {name:"HelloServerWorld"};
console.log(data)
$.getJSON("http://localhost:8081/next-user", data, function(response){
console.log(response);
});
The server always echos an empty JSON object regardless of what I send to it where the client can see that the object is populated. I didn't want to post the entirety of the code but if it's needed then let me know, but I'm fairly confident the problem lies in what I've given so far, I'm just not sure where
GET requests don't include "req.body", req.body is used with POST requests. You may want to pass data in the form of a parameter, e.g. " "/next-user/:id" or using a POST request instead to pass over the data!
Replace console.log with res.end and you should get back the request.
This is my code:
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/',function(req,res){
res.sendfile("index.html");
});
app.post('/login',function(req,res){
console.log(req.body)
var user_name=req.body.user;
var password=req.body.password;
console.log("User name = "+user_name+", password is "+password);
res.end("yes");
});
app.listen(3000,function(){
console.log("Started on PORT 3000");
})
I set a console in post method function but it displayed:
{}
User name = undefined, password is undefined
I couldn't get any data from response. I had lookup doc but I couldn't find any error. I don't still understand. What mistake did i make?
You should send your data using Postman with the content-type:application/json in the header. This will ensure that the server receives the data in JSON format.
I know what happens!
If I want to send data in form-data, here has good solution helps me:
Result is undefined when I use form-data in Postman
If I use x-www-form-urlencoded to send data, it's actually valid!
I find when postman send json data without content-type:application/json in headers, it's definitely error. Now, I have added it. It can be valid.
Thanks, everyone!
I just installed the latest versions of modules. I can not get any GET or POST variables. What i do wrong?
NODE: v0.12.2
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
});
app.listen(3000,function(){
console.log("Started on PORT 3000");
})
http://localhost:3000/?token=devvvvv GET returns:
you posted:
{}
Thanks for answers, but problem with POST does not solved...
POST token=as123ds on http://localhost:3000/ return empty array in req.body
How can i solve this?
You are submitting parameters via the query string and attempting to access them through the request body which in this case is empty.
The token parameter will be available in request.query like so:
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.query.token, null, 2))
});
If you only plan to submit parameters in the query string you should not need to mount the body-parser middleware at all.
You should be using the req.query:
req.query
An object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.
api link
You are parsing JSON from the request, so the POST from client must have 'Content-Type': 'application/json' in HTTP header. If not, you'll have empty request.body at server side.
bodyparser module requires the http request's "Content-type" property equal to "application/json". It won't work for other values.
You have to check the request content type in the client, this link may help
Node (Express) request body empty
This is because bodyParser parses application/json,
application/x-www-form-encoded and multipart/form-data, and it selects
which parser to use based on the Content-Type.