-1

This is my Flask code:

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def signal():
    if request.method == 'POST':
        content = request.get_json()
        return content
    else:
        return 'Hello, world!'

if __name__ == '__main__':
    app.run(debug=True)

I run this HTTP Post (from the same machine):

import requests
import json
dump= '{"on":"true"}'
r = requests.post('http://127.0.0.1:5000', dump)

And receive this error:

[2017-03-04 14:18:35,250] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\flask\app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\flask\app.py", line 1725, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response

Any ideas on why this is occuring? From reading online at first I thought it was because I didn't have an else in case of it being a GET, but I can't understand now.

duan
  • 8,515
  • 3
  • 48
  • 70
Dorothy
  • 61
  • 4
  • 11

2 Answers2

1

If you are testing returning your post request payload directly, you should do jsonify and flask has this util function

from flask import Flask
from flask import request, jsonify

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def signal():
    if request.method == 'POST':
        content = request.get_json()
        return jsonify(content)
    else:
        return 'Hello, world!'

if __name__ == '__main__':
    app.run(debug=True)

Then in your testing code, add Content-Type into header, (without this, request.get_json() won't work):

dump = '{"on":"true"}'
r = requests.post('http://127.0.0.1:5000', data=dump,
                  headers={'Content-Type': 'application/json'})
duan
  • 8,515
  • 3
  • 48
  • 70
  • This wouldn't trigger the error though. – vallentin Mar 04 '17 at 14:32
  • @Vallentin It will, because if there's no payload, `content` is a `NoneType`, Flask does not allow `NoneType` as return value – duan Mar 04 '17 at 14:44
  • That is true. However considering the condition of the testing, then `content` shouldn't be a `NoneType`. – vallentin Mar 04 '17 at 14:46
  • @Vallentin aha, there is a second mistake here. No `Content-Type` specified in the request so `request.get_json() ` won't work – duan Mar 04 '17 at 15:03
  • Ohh nice catch! +1 from me then! :) – vallentin Mar 04 '17 at 15:04
  • Thanks! Works great! The only thing that slightly confuses me is how to actually display the JSON/ write it to a file. I thought it would display in the cmd, but it does not. I'm probably not understanding Flask, so I'm sorry. – Dorothy Mar 04 '17 at 15:11
  • @AndrewBlair just print them when you want – duan Mar 04 '17 at 15:39
  • @Duan i cannot get this to work. `r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", jsonify(content))` in the signal() if request method post – Dorothy Mar 04 '17 at 15:58
0

Looks like - issue is with your script which is making request to your application. Try with curl, something like below:

$ curl -H "Content-type: application/json" -X POST http://127.0.0.1:5000 -d '{"on":"true"}'

You will get response as below:

{
  "on": "true"
}

Your code (added jsonify) should look as below:

from flask import Flask
from flask import request,jsonify 

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def signal():
    if request.method == 'POST':
        content = request.json
        return jsonify(content)
    else:
        return 'Hello, world!'

if __name__ == '__main__':
    app.run(debug=True)
Sopan
  • 644
  • 7
  • 13
  • Should the response be displaying in my terminal? – Dorothy Mar 04 '17 at 15:35
  • Yes! you are using two terminals. One to your flask application and the other to run curl command. Curl will respond your JSON object. – Sopan Mar 06 '17 at 08:57