[英]Getting null from flask request python
我正在編寫一個簡單的flask應用程序,根據我的查詢,我應該以所需的格式獲得所需的答案。 代碼如下:
#-*- 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)
它運行正常,但響應為空。 我想再創建30個以上的問題,並將值存儲在store
數組中。 但是,我認為價值並沒有被附加到store
內部。
不過,在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)]
為什么在flask中,值未附加? 我正在郵遞員中測試我的應用程序。 請指導我缺少的地方。
如果不提供Post方法的數據類型,則request.form的計算結果為
ImmutableMultiDict([('{"question": "What is the number of total observation of the dataset"}', u'')])
和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 -X POST -d '{"question": "What is the number of total observation of the dataset"}' http://127.0.0.1:5000/api/conversation/
[“形狀”]
$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/
[“形狀”]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.