0

POST plain text (from another API) simulate with postman using plain text

{ "name":"brad" , "address":"mystreet" }

on Node:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json());

app.post('/api/test', function (req, res) {
    var contact = req.body; 
    console.log(contact);
}); 

Got these on the console:

{ '{\n"name":"brad",\n"address":"mystreet"\n}': '' }

How to make use or convert those text into a JSON data (formatted)?.

4
  • 1
    What do you mean by "a JSON data (formatted)"? You either parse the JSON (How to parse JSON using Node.js?), which makes it an object, or you have a string, which you can pretty print (How can I pretty-print JSON using JavaScript?). That output is odd, because it's actually and object, with your JSON string as the name of a property... Commented Mar 25, 2018 at 16:21
  • I suspect this is an issue with how you are sending the JSON vis Postman, not an issue with the express code. Commented Mar 25, 2018 at 16:22
  • When I'm using `console.log(typeof(req.body)); it print object not a text (NOT plain text). Commented Mar 25, 2018 at 16:32
  • When I'm added `var str = JSON.stringify(obj, null, 2); it return { "{\n\"name\":\"brad\",\n\"address\":\"mystreet\"\n}": "" } Commented Mar 25, 2018 at 16:40

1 Answer 1

1

JSON.parse parses valid JSON text into JS objects:

var text = '{ "name":"brad" , "address":"mystreet" }';

var json = JSON.parse(text);

console.log(json);

Sign up to request clarification or add additional context in comments.

1 Comment

Should you need this when using bodyParser.json()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.