Reputation: 1115
I've got this boilerplate code:
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
text = request.get_json()
return jsonify({'you sent:': text}), 201
else:
return jsonify({'about': 'hakuna matata'})
@app.route('/multi/<int:num>', methods=['GET'])
def get_multiply10(num):
return jsonify({'result': num*10})
if __name__ == '__main__':
app.run(debug=True)
when I jump into Python interpreter I can interact with the API with GET like this:
from requests import get, post
get('http://localhost:5000').json()
and it returns expected output, but when I try to execute POST request:
post('http://localhost:5000', data={'data': 'bombastic !'}).json()
I get returned None
from the text
variable, meaning POST request is being acknowledged but the data isn't making it through to the variable.
What am I doing wrong ?
Upvotes: 0
Views: 204
Reputation: 1072
Apart from your case, many websites require JSON-encoded data. So, you'll have to encode it before posting:
r = requests.post(url, data=json.dumps(payload))
Alternatively, you can use the json
parameter...
r = requests.post(url, json=payload)
... and let requests
encode it for you.
Upvotes: 1