【问题标题】:Sending a POST request to RESTFUL API(Flask), but receiving a TypeError向 RESTFUL API(Flask) 发送 POST 请求,但收到 TypeError
【发布时间】:2023-03-14 09:47:01
【问题描述】:

所以我试图从我的流式应用程序向我的 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,然后它会使用这些值,返回一个数据帧,并将数据帧发送到流光应用程序。

非常感谢任何帮助。

【问题讨论】:

  • create_row_data 来自哪里?
  • @KlausD.,我在 app.py 中打错了字。现在row_data 应该是create_row_data

标签: python flask flask-restful


【解决方案1】:

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

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

【讨论】:

  • 嗨@djnz,有没有办法让我在request.method== 'POST' 时看到我的print(create_row_data)response.content
  • 当它是一个POST 请求时,您应该会看到这个。虽然你在POST 之前在app.py 中调用GET,但它还没有达到这一点:api_url = requests.get("http://127.0.0.1:5000/") # Flask url
  • 我为我的api_url 删除了requests.get,但我仍然得到ok,就像create_row_data 的内部内容一样?
猜你喜欢
  • 2018-07-05
  • 2021-09-19
  • 2015-01-26
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多