JSON 数据 node.js

JSON data with node.js

我是 node js 编程的新手,我已经开始开发 RESTful API。现在,当我 POST 来自终端的数据时,像这样卷曲:

curl -H "Content-Type: application/json" -X POST -d '{"full_name":"Hristijan ilieski", "email":"email@mail.com", "password":"123"}' http://localhost:3000/api/login

我在请求正文中获得了正确的数据。 但是,当我尝试从 hurl.it 或其他一些服务 post 一些 json 时,请求正文是空的。有什么区别,问题出在哪里?这是我的代码:

function registerUser(req, res){
var jsonData = JSON.parse(JSON.stringify(req.body));
//jsonData['key'] should contain the value...
}

这是来自 hurl.it 请求的图像:

hurl request

编辑

我不知道我做了什么,但现在当我尝试 POST 来自 hurl.it 的请求时,我收到此错误:

SyntaxError: Unexpected token f at parse (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/lib/types/json.js:83:15) at /home/hristijan/Documents/UrbanAPI/node_modules/body-parser/lib/read.js:116:18 at invokeCallback (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:262:16) at done (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:308:7) at emitNone (events.js:67:13) at IncomingMessage.emit (events.js:166:7) at endReadableNT (_stream_readable.js:905:12) at nextTickCallbackWith2Args (node.js:441:9) at process._tickCallback (node.js:355:17)

你为什么要 strinfigying 然后 JSON.parsing 请求 body?这让你回到第一个问题......

此外,您可能需要一些 accept headers 和 utf-8 编码,具体取决于您的后端设置方式...(后者之前已经回来困扰我。)

不妨试试 Content-type: application/json;字符集=utf-8

请尝试使用 hurl.it 主体而不是像这样的参数:

全身是:

{
  "full_name":"Pero Perov",
  "email":"pero@mail.com",
  "password":"pero123",
  "mobile_number":"076 543 210",
  "home_address":"Perovaca 16b",
  "work_address":"Merovaca 18c"
}