Ensure bodyParser.urlencoded doesn't transform request body to json
See original GitHub issueFrom #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:
- Created 9 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How about just
router.use(bodyParser.text({ type: 'urlencoded' }))
? It should provide you the URL-encoded body as a string inreq.body
, then.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: