node.js 如何使用 Advanced REST Client 或 Postman 测试 Express/node REST API 后端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11688608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test Express/node REST API backend with Advanced REST Client or Postman?
提问by ragulka
I need to test my REST API backend that accepts JSON with the Advanced REST Client or Postman for Chrome.
我需要使用 Advanced REST Client 或 Postman for Chrome 测试接受 JSON 的 REST API 后端。
But I am running into issues: I can only send the request using the built-in form and using Content-Type: application/x-www-form-urlencoded
但我遇到了问题:我只能使用内置表单和使用 Content-Type: application/x-www-form-urlencoded 发送请求
But this will not work since I have embedded documennts, for example, I need to POST this:
但这不起作用,因为我已经嵌入了文档,例如,我需要发布这个:
{title:"Awesome post!", tags: ["blue", "jeans"] }
This is not possible with the built-in forms of either Chrome extension.
这对于 Chrome 扩展程序的内置形式是不可能的。
When I select Raw Body and insert the content there, my backend sees the req.body as being an empty object. When I also set the header "Content-Type: application/json", I get the following error in my backend:
当我选择 Raw Body 并在那里插入内容时,我的后端将 req.body 视为一个空对象。当我还设置标题“Content-Type:application/json”时,我的后端出现以下错误:
SyntaxError: Unexpected token n
at Object.parse (native)
at IncomingMessage.exports.parse.application/json (/Library/WebServer/Documents/slipfeed/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:135:16)
at IncomingMessage.EventEmitter.emit (events.js:85:17)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socket.ondata (http.js:1682:22)
at TCP.onread (net.js:404:27)
Note: I am using bodyParser() and methodOverride() in my app's configuration. Disableing them did not help.
注意:我在我的应用程序配置中使用 bodyParser() 和 methodOverride()。禁用它们并没有帮助。
What settings should I use so that I could just enter the JSON to the Raw body field and the request would work?
我应该使用什么设置,以便我可以将 JSON 输入到 Raw body 字段并且请求会起作用?
To clarify the answer: I had to set both Content-Type: application/json (in request header) and use well-formed json where property names are also inside double quotes to get it working.
为了澄清答案:我必须同时设置 Content-Type: application/json (在请求标头中)并使用格式良好的 json,其中属性名称也在双引号内以使其工作。
回答by awl
Try enclosing the field properties in quotes: {"title":"Awesome post!", "tags": ["blue", "jeans"] }
尝试用引号将字段属性括起来:{"title":"Awesome post!", "tags": ["blue", "jeans"] }

