0
votes

I am using node.js to send HTTP POST to an APP server. The email address [email protected] in POST body shows up as joe%40gmail.com on APP server side. I used Content-Type text/plain in the following code but application/x-www-form-urlencoded is actually in the POST request header, according to the server log.

My earlier post has a mistake on Chrome postman results. Please ignore it, and sorry for the confusion.

I am new to this, and may have missed something obvious here. Any help is appreciated.

Here is the code:

var express = require("express");
var app = module.exports = express();
var request = require('request');
var config = require('../config'); 
app.post('/', function(req, res) {

    var username = req.body.name;
    var password = req.body.passwd;
    var email = req.body.email;
    console.log("email=", email);

    // connect to APP server
    var url = config.appServer;

    request( {
        uri: url,
        method: "POST",
        timeout: 10000, // 10 sec
        headers: {
            'Content-Type': 'text/plain'
        },
        form: {
            act: 'create',
            username : username,
            passwd: password,
            email: email
        }
        }, function (error, response, body) {
            // handle return here
1
Looks like it's url-encoded, did you try just decodeURIComponent() it ? - adeneo
Looks like it's an issue with your other app, not this one, since the console.log outputs the right string. Update the question with the code of the /account post handler - gfpacheco
No, I haven't tried decodeURIComponent(). I can use Chrome, safari to send HTTP GET request with the same string. They all show up correctly on APP server side. I probably missed something in the header of node.js request, but don't know what. - gfc
gfpacheco & adeneo, Thank you for your comments. Given the content-type x-www-form-urlencoded, the above results are understandable. The remaining question is why the header is incorrect from the code above. I've corrected a mistake on postman in the earlier question. - gfc

1 Answers

0
votes

That's completely normal behavior. Also you don't need to set the content-type header when using the form option with request.

Here is a short example:

request({
  uri: 'http://requestb.in/1g2xzno1',
  method: 'POST',
  timeout: 10000, // 10 sec
  form: {
    act: 'create',
    username : 'username',
    passwd: 'password',
    email: '[email protected]'
  }
  }, function (err, res, body) {
    if (err) console.log(err)
    console.log(body)
  })

You can see the results from that request here. This is what your app server receives. Then all you need to do is parse the request body on your app server (it's the string below the RAW BODY header):

var qs = require('qs')
qs.parse('act=create&username=username&passwd=password&email=joe%40gmail.com')

{ act: 'create',
  username: 'username',
  passwd: 'password',
  email: '[email protected]' }

Alternatively if you are using Express on your app server you can use the body-parser middleware that will do the same for you:

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