0

I passing a request from python to flask. I am not able to access the requests which are being sent from python in flask. Here is my python function -

import requests

dice_roll = 1
dealear_roll = 2

url = 'http://127.0.0.1:5000/dice'
data = {'dice_roll':dice_roll,'dealear_roll':dealear_roll}
r = requests.post(url=url,data=data)

Here is flask api

from flask import Flask, render_template
import random
from flask import request

app = Flask(__name__)

@app.route('/dice')
def dice_roll():

    dice_roll = request.args.get('dice_roll')
    dealear_roll = request.args.get('dealear_roll')

    print('dice_roll',dice_roll)
    print('dealear_roll',dealear_roll)



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

I am not able to access the requests in flask. Can anyone tell where am i doing wrong?

merkle
  • 1,585
  • 4
  • 18
  • 33

2 Answers2

1

you should use request.form.get instead of request.args.get

from flask import Flask, render_template
import random
from flask import request

app = Flask(__name__)

@app.route('/dice', methods=['GET', 'POST'])
def dice_roll():
    if request.method == 'POST':
        dice_roll = request.form.get('dice_roll')
        dealear_roll = request.form.get('dealear_roll')

        print('dice_roll', dice_roll)
        print('dealear_roll', dealear_roll)
    return ''



if __name__ == '__main__':
    app.run(debug=True)
Nihal
  • 5,262
  • 7
  • 23
  • 41
0

You need to add method handler in the route of GET, POST like this..

@app.route('/dice', methods=['GET', 'POST'])
def dice_roll():

    dice_roll = request.args.get('dice_roll')
    dealer_roll = request.args.get('dealer_roll')

    print('dice_roll',dice_roll)
    print('dealer_roll',dealer_roll)
P.hunter
  • 1,345
  • 2
  • 21
  • 45