How to get text content body?

4,475 views
Skip to first unread message

O haya

unread,
Nov 10, 2016, 11:39:52 AM11/10/16
to Express
Hi,

I am really new to Express, and am trying to implement an app that can process HTTP POST requests with content body that is just text.  I have been trying to use Node.js + Express and bodyParser, but when I output the req.body, it is just outputting:

Here's my code:

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

var app = express()

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))


app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.post('/foo', function (req, res) {
  console.dir("req.body=[" + req.body + "]");
  res.send('Hello World!' + req.body);
})


When I do a post to the /foo URI with "Hi there" in the body, I get:

Example app listening on port 3000!
'req.body=[[object Object]]'



In the client (Firefox + RESTclient plugin), I get:


Hello World![object Object]



I need to get the POSTed content body as a String so that I can process it.

So how can I get that content body?  Shouldn't I be able to do that using Express and the text bodyParser?

Thanks in advance!!

Jim


Jason Crawford

unread,
Nov 10, 2016, 11:49:34 AM11/10/16
to expre...@googlegroups.com
This is just how JavaScript renders objects. Try until.inspect instead, that will be more helpful

-Jason via iPhone
--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at https://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

O haya

unread,
Nov 10, 2016, 12:23:39 PM11/10/16
to Express
Hi,

Did you mean "util.inspect()"?

I changed the log.console to:

  console.log("req.body=[" + util.inspect(req.body, {showHidden: true}) + "]");


and got output:

req.body=[{}]

Jim

O haya

unread,
Nov 10, 2016, 12:37:21 PM11/10/16
to Express
Hi,

FYI, I think that I have kind of figured out what might have been the problem.  I think that the RESTclient plugin might've been automatically putting some kind of "Content-type" header into the POST requests that I wasn't able to see.

I tried using a different plugin for Firefox ("HTTP Tool") and when I used that, my app was able to see the incoming body.

Thanks,
Jim

O haya

unread,
Nov 10, 2016, 12:48:25 PM11/10/16
to Express
FYI, I think that it wasn't working because it was sending in the content-type WITH a charset, e.g.:

I think I just figured out exactly what was wrong with the content-type that soapui  was sending in...

text/plain;charset=UTF-8

I guess that brings up another question:  Is there a way to call the "app.use(bodyParser.text({ type: 'text/html' }))" so that it will accept any, or no, charset (kind of like wildcard the charset part)?

Thanks,
Jim

Jason Crawford

unread,
Nov 10, 2016, 1:12:08 PM11/10/16
to expre...@googlegroups.com
Yes, I meant util.inspect(). That shows you that your req.body is an empty object, which renders as {}

The charset shouldn't matter. But you do need to make sure you have the right bodyParser. Depending on the client/browser, params could conceivably be passed as JSON, form params (application/x-www-form-urlencoded), or maybe something else. Each may require a different bodyParser, or the body won't get parsed.

To unsubscribe from this group and stop receiving emails from it, send an email to express-js+unsubscribe@googlegroups.com.

To post to this group, send email to expre...@googlegroups.com.
Visit this group at https://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.



--
Fieldbook: Create a database, as easily as a spreadsheet

O haya

unread,
Nov 20, 2016, 8:44:19 AM11/20/16
to Express
Hi,

Sorry, it's been awhile since I posted about this, but I wanted to mention something that I just found, in case it helps anyone in the future.

So I was testing a new client that I want to send requests to my Node.JS+Express.JS app since yesterday, and was running into the same empty body problem I head earlier.

In this new app, it was sending header;

Content-Type:  text/xml;charset=UTF-8;

but even a simple express app was not seeing th body.

Then, comparing to sending the same request body via SOAPUI, I noticed from the SOAPUI logs that the header SOAPUI was sending was:

Content-Type:  text/xml;charset=UTF-8

Notice the difference?  Yes, SOAPUI wasn't sending the last semicolon after the "UTF-8" part!

So, I modified the new client to also send:

Content-Type:  text/xml;charset=UTF-8

and then my Express app could see the body!

In other words, having the semicolon (";") at the end of the "charset=UTF-8" part of the Content-Type HTTP header caused the body parser not to be able to see the request body.

Jim


Reply all
Reply to author
Forward
0 new messages