21

I need to test my REST API backend that accepts JSON with the Advanced REST Client or Postman for Chrome.

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

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.

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:

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.

What settings should I use so that I could just enter the JSON to the Raw body field and the request would work?

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.

ragulka
  • 4,312
  • 7
  • 48
  • 73

1 Answers1

27

Try enclosing the field properties in quotes: {"title":"Awesome post!", "tags": ["blue", "jeans"] }

awl
  • 1,540
  • 1
  • 15
  • 18
  • 1
    Actuall, this seems to do the trick. i had removed the Content-Type: application/json from headers. It works now, brilliant :) – ragulka Jul 27 '12 at 13:46
  • @ragulka: Can you please tell me how you did it? I have added header but it is still giving me error. Also, when I send in as "application/x-www-form-urlencoded" two content-type header are being set. – Razort4x Apr 04 '14 at 12:00
  • @Razort4x, Any REST Client like Postman should give you the option to set a header. In Postman specifically there is a button that says "Headers" and it will let you set headers which will even auto complete for you. – Ryan Ore May 17 '14 at 17:38