Yagel
Yagel

Reputation: 1312

Flask restful - Can't see output from json post

I'm using flask to create api server, which get post of json data. I used following this tutorial to create the code: from flask import Flask from flask import request

app = Flask(__name__)

@app.route('/postjson', methods = ['POST'])
def postJsonHandler():
    print (request.is_json)
    content = request.get_json()
    print (content)
    return 'JSON posted'

app.run(host='0.0.0.0')

When I run:

curl -X POST http://127.0.0.1:5000/postjson -H "Content-type: application/json" -d '{ "data": { "url": "https://google.com" }}'

I just see "JSON posted", without any print. Why can't I see any data? I also tried to use POSTMAN, but same result.

I also tried the json in the example of the guide:

{ 
 "device":"TemperatureSensor", 
 "value":"20", 
 "timestamp":"25/01/2017 10:10:05" 
}

also the same.

EDIT- as @TomMP answer, when I tried the following code:

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/producer', methods = ['POST'])
def postJsonHandler():
    print (request.is_json)
    content = request.get_json()
    print (content)
    return request.get_json()
    #return 'JSON posted'

app.run(host='0.0.0.0')

I get:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

And When I try the debug mode, I get:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>TypeError: 'dict' object is not callable
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict. // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css"
        type="text/css">
... (more lines of data)

Upvotes: 0

Views: 1966

Answers (3)

dodopy
dodopy

Reputation: 169

that because you only return text 'JSON posted'
so return what you want to get
like json response:

return jsonify({'status': 0, 'msg': 'success'})

detail

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/postjson', methods = ['POST'])
def postJsonHandler():
    content = request.json
    print(content)
    return jsonify(content)

app.run(host='0.0.0.0')

call example:

requests.post('http://0.0.0.0:5000/postjson', json={'a':'b'}).json()

Upvotes: 1

TomMP
TomMP

Reputation: 815

When you use curl to access a route, it will only show you what that route returned - in this case, that's JSON posted. It won't show you the print statements that are in between. You could try and run flask in debug mode. That should print out to the console where you're running this app from.

Edit: To be clear, you still won't receive the data you send as an answer to your request, i.e. in Postman. For this, you will have to return the data at the end of your function using return request.get_json()

Upvotes: 0

dmitrybelyakov
dmitrybelyakov

Reputation: 3864

When you use print() it simply prints everything to console, so check it for you running app to see printed output. What you return ('JSON posted') from your view is what gets sent back to the client as a response.

Upvotes: 0

Related Questions