【发布时间】:2016-09-03 03:27:15
【问题描述】:
我是 node js 编程的新手,我已经开始开发 RESTful API。现在,当我使用 curl 从终端发布数据时,如下所示:
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 或其他服务发布一些 json 时,请求正文为空。有什么区别,问题出在哪里?这是我的代码:
function registerUser(req, res){
var jsonData = JSON.parse(JSON.stringify(req.body));
//jsonData['key'] should contain the value...
}
这是来自 hurl.it 请求的图片:
编辑
我不知道我做了什么,但是现在当我尝试从 hurl.it 发布请求时,我收到了这个错误:
SyntaxError: Unexpected token f 在解析(/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/lib/types/json.js:83:15) 在/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/lib/read.js:116:18 在 invokeCallback (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:262:16) 完成后(/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:251:7) 在 IncomingMessage.onEnd (/home/hristijan/Documents/UrbanAPI/node_modules/body-parser/node_modules/raw-body/index.js:308:7) 在 emitNone (events.js:67:13) 在 IncomingMessage.emit (events.js:166:7) 在 endReadableNT (_stream_readable.js:905:12) 在 nextTickCallbackWith2Args (node.js:441:9) 在 process._tickCallback (node.js:355:17)
【问题讨论】:
-
hurl.it 中是否设置了标头 Content-Type:application/json?这可能是问题
-
是的,我已经设置了 Content-Type: application/json... 我什至尝试了一些来自 hurl.it 的不同服务,但它不起作用。它仅在我尝试从 curl 发布请求时才有效..
-
您能否附上您的 hurl.it 请求的打印屏幕,以便我查看是否有任何差异?
标签: javascript json node.js curl http-post