question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ensure bodyParser.urlencoded doesn't transform request body to json

See original GitHub issue

From #39 How can I ensure the raw request body is what is sent? Here’s what I’m toying with:

var bodyParser = require('body-parser');

router.use(function(req, res, next) {
    var data = '';
    req.on('data', function(chunk) {
            data += chunk;
        }); 
    req.on('end', function() {
            req.rawBody = data;
            next();
        }); 
});
// retrieve all request body objects
router.use(cookieParser());
router.use(bodyParser());
router.use(methodOverride());

router.post('/login', function(req, res) {
  console.log(req.rawBody);
  if (!req.body) return res.sendStatus(400);

  var options = httpconn.httpOptions({
    resource: 'uaa',
    resourcePath: '/uaa/login.do',
    method: 'POST',
    headers: {'Referer': httpconn.buildUri('uaa') + '/uaa/login'}
  });
  var httpRequest = application.httprequest(options, function(response) {
      if(response.statusCode == 302) {
        res.send(response.headers);
      } 
  });
  httpRequest.write(qs.stringify(req.rawBody));
  httpRequest.end();

To use the req.rawBody middleware, I needed to use -H 'Content-Type: text/plain'.

I want to ensure that a request with application/x-www-form-urlencoded maintains the request body as username=myname&password=mypass without transforming to {'username':'myname','password':'mypass'}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Feb 23, 2015

How about just router.use(bodyParser.text({ type: 'urlencoded' }))? It should provide you the URL-encoded body as a string in req.body, then.

0reactions
kagan94commented, Mar 24, 2018

In my case, the content-type of incoming requests was ‘text/plain’, so on the backend side I needed to parse it from that format:

app.use(parser.json({type: 'text/plain'}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

The JSON data in request body is not getting parsed using ...
Both .json() and .urlencoded() look at the headers of a request and based on the Content-Type the right middleware is used. I am ......
Read more >
Express.js express.urlencoded() Function - GeeksforGeeks
urlencoded () function is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body- ......
Read more >
Java Body Parsers - 2.3.x - Play Framework
A body parser transforms this request body into a Java value. Note: You can't write BodyParser implementation directly using Java. Because a Play...
Read more >
Express body-parser middleware
Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
Read more >
Get HTTP POST Body in Express.js - Stack Abuse
use(bodyParser.urlencoded({ extended: true })); app.post('/post-test', (req ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found