Manu Gupta Manu Gupta - 1 year ago 199
TypeScript Question

Http post request in Angular2 does not pass parameters

I am trying to send a parameter using Angular2 POST to my Python/Tornado back-end which returns a JSON object. The parameters are being sent properly but at the Python side, it is returning 400 POST missing arguments error. I am using Ionic 2/Angular2 in the front-end and Python/Tornado server.

Angular2 code is as follows:
Here content is a variable containing HTML table

let body = JSON.stringify({content: content});
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });, body, options).map(res => res.json()).subscribe(data => {
}, error => {

Python code is as follows:

def post(self):
print self.request.arguments
print self.get_argument('content')
self.finish(dict(result="ok", data=content))

Here is the error:

[W 160824 06:04:30 web:1493] 400 POST /test ( Missing argument content
[W 160824 06:04:30 web:1908] 400 POST /test ( 1.67ms

Answer Source

Your Angular2 code looks reasonable, however your Python code is wrong, because you are treating the request as x-www-form-urlencoded. You have to access the JSON string through the request.body property:

data = tornado.escape.json_decode(self.request.body)

See for an answer to a similar question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download