Reputation: 49
I am getting a string through a prompt and I want to send it from the client to my server (Which uses Express).
Client:
username = prompt('Enter your Username');
req.open('POST', url + 'username');
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencode;charset=UTF-8');
req.send(username);
Server:
app.use(bodyParser.urlencoded({
extended: false
}));
app.post('/username', function(req, res) {
console.log(req.body);
res.end('ok bud');
});
And the result of req.body is always {} when the username var is something like 'test' or something. If anyone can tell me what I am doing wrong and how to fix it that would be great.
Upvotes: 1
Views: 40
Reputation: 61
You've set your content type to 'application/x-www-form-urlencode;charset=UTF-8' Which requires the payload to be in key value pairs.
Since you want to post plain text, set your content type to 'text/plain' instead.
Upvotes: 1
Reputation: 56
You are not attaching username in your post request at client side which was taken through prompt.
Upvotes: 0