向 RESTFUL API(Flask) 发送 POST 请求,但收到 TypeError

时间:2021-07-07 20:52:15

标签: python flask flask-restful

所以我试图从我的流媒体应用程序向我的 Flask API 发送某些值,但我不确定为什么我会收到类型错误(使用它作为参考:Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request)。

类型错误:'get_data' 的视图函数没有返回有效的响应。函数要么返回 None 要么在没有返回语句的情况下结束

app.py

import requests
import streamlit as st
...
api_url = requests.get("http://127.0.0.1:5000/") # Flask url
create_row_data = {'name': name, 'type': get_type(name), 'token_value': token_value, 'external': external, 'start': start, 'end': end, 'step': step}
print(create_row_data)
r = requests.post(url=api_url, json = create_row_data)

main.py 中的 urlhttps

ma​​in.py

@app.route('/', methods=['GET', 'POST'])
def get_data():
   if request.method =='POST':
    p_name = request.json['name']
    p_type = request.json['type']
    ...
    p_end = request.json['end']
    p_step = request.json['step']
    create_row_data = {'p_name': str(p_name), 'p_type': str(p_type), ... , 'p_end': str(p_end), 'p_step': str(p_step)}
    print(create_row_data)
    response = requests.post(url, data=json.dumps(create_row_data), headers= {'Content-type': 'application/json'}
    return response.content

我想要做的是将值发送到我的 Flask API,然后 Flask API 将使用这些值,返回数据帧,并将数据帧发送到流光应用程序。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的路由不会为 GET 请求返回任何内容(return response.contentPOST 内处理)。

例如如果您在 return 'ok', 200 处理程序下方添加 POST,它将处理 GET 请求