[英]Getting null from flask request python
I am writing a simple flask application where based on my query, I should get the required answer in the desired format. 我正在编写一个简单的flask应用程序,根据我的查询,我应该以所需的格式获得所需的答案。 The code is as below;
代码如下:
#-*- coding: utf-8 -*-
import StringIO
import os
import pandas as pd
import numpy as np
from flask import Flask, request, Response, abort, jsonify, send_from_directory,make_response
import io
from pandas import DataFrame
import urllib2, json
import requests
from flask import session
import sys
reload(sys)
sys.setdefaultencoding("ISO-8859-1")
app = Flask(__name__)
@app.route("/api/conversation/", methods=['POST'])
def chatbot():
df = pd.DataFrame(json.load(urllib2.urlopen('http://192.168.21.245/sixthsensedata/server/Test_new.json')))
question = request.form.get('question')
store = []
if question == 'What is the number of total observation of the dataset':
store.append(df.shape)
if question == 'What are the column names of the dataset':
store.append(df.columns)
return jsonify(store)
if __name__ == '__main__':
app.debug = True
app.run(host = '192.168.21.11',port=5000)
It's running properly but getting null response. 它运行正常,但响应为空。 I would like to create ~30 more questions like this & store values in the
store
array. 我想再创建30个以上的问题,并将值存储在
store
数组中。 But values are not getting appended inside store
, I think. 但是,我认为价值并没有被附加到
store
内部。
In jupyter notebook, though, I am getting proper response; 不过,在jupyter笔记本中,我得到了适当的答复;
df = pd.DataFrame(json.load(urllib2.urlopen('http://192.168.21.245/sixthsensedata/server/Test_new.json')))
store = []
store.append(df.shape)
print store
[(521, 24)]
Why in flask, the values are not getting appended? 为什么在flask中,值未附加? I am testing my application in postman.
我正在邮递员中测试我的应用程序。 Please guide where I am lacking.
请指导我缺少的地方。
When not providing the data type for the Post method, request.form evaluates to 如果不提供Post方法的数据类型,则request.form的计算结果为
ImmutableMultiDict([('{"question": "What is the number of total observation of the dataset"}', u'')])
and question = request.form.get('question')
ends up being none You can explicitly use content type as json, or force load it. 和
question = request.form.get('question')
最终都不是。您可以显式使用内容类型作为json,或强制加载它。
@app.route('/api/conversation/', methods=['POST'])
def chatbot():
question = request.get_json(force=True).get('question')
store = []
if question == 'What is the number of total observation of the dataset':
store.append("shape")
elif question == 'What are the column names of the dataset':
store.append("columns")
return jsonify(store)
Curl requests 卷曲请求
$curl -X POST -d '{"question": "What is the number of total observation of the dataset"}' http://127.0.0.1:5000/api/conversation/
["shape"] [“形状”]
$curl -H 'Content-Type: application/json' -X POST -d '{"question": "What is the number of total observation of the dataset"}' http://127.0.0.1:5000/api/conversation/
["shape"] [“形状”]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.