I have a simple http client in python that send the http post request like this:
import json
import urllib2
from collections import defaultdict as dd
data = dd(str)
req = urllib2.Request('http://myendpoint/test')
data["Input"] = "Hello World!"
response = urllib2.urlopen(req, json.dumps(data))
On my server side with Flask, I can define a simple function
from flask import request
@app.route('/test', methods = ['POST'])
def test():
output = dd()
data = request.json
And the data
on server will be the same dictionary as the data
on the client side.
However, now I am moving to Klein, so the server code looks like this:
@app.route('/test', methods = ['POST'])
@inlineCallbacks
def test(request):
output = dd()
data = request.json <=== This doesn't work
and the request that's been used in Klein does not support the same function. I wonder is there a way to get the json in Klein in the same way I got it in Flask? Thank you for reading this question.
Asaik Klein doesn't give you direct access to the json data, however you can use this code to access it:
import json
@app.route('/test', methods = ['POST'])
@inlineCallbacks
def test(request):
output = dd()
data = json.loads(request.content.read()) # <=== This will work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With