1

So I am trying to send certain values to my Flask API from my streamlit application, but I am not sure as to why I am getting a type error(Using this as reference: Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request).

Type error: The view function for 'get_data' did not return a valid response. The function either returned None or ended without a return statement

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)

The url in the main.py has https

main.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

What I want to do is send the values to my Flask API which would then use those values, return a dataframe, and send the dataframe to the streamlit application.

Any help is greatly appreciated.

SL42
  • 201
  • 3
  • 17

1 Answers1

0

Your route does not return anything for a GET request (return response.content is handled within the POST).

e.g. if you add return 'ok', 200 below your POST handler, it will handle the GET request

djnz
  • 2,021
  • 1
  • 13
  • 13
  • Hi @djnz, is there a way where I can see my `print(create_row_data)` or the `response.content` when `request.method== 'POST' `? – SL42 Jul 07 '21 at 21:19
  • when it is a `POST` request you should see this. It's not reaching this point though as you're calling the `GET` before the `POST` in `app.py`: `api_url = requests.get("http://127.0.0.1:5000/") # Flask url ` – djnz Jul 07 '21 at 23:09
  • I got rid of the `requests.get` for my `api_url` and I am still getting `ok` as supposed to what is inside of `create_row_data`? – SL42 Jul 08 '21 at 00:25