0

URL: http://127.0.0.1:5000/login?apple=exec&basket=app

Apple value returns as exec but basket value returns as None instead of app. Problem with picking up second value

from flask import Flask, request, render_template

app = Flask(__name__)

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

        a = str(request.args.get('apple', type = str))
        b = str(request.args.get('basket', type = str))
        return (b)

if __name__ == "__main__":
    app.run()
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
Jay
  • 11
  • 2
  • What version of python are you running, and what version of Flask? – CodeLikeBeaker Aug 09 '18 at 14:49
  • Flask 1.0.2 Python 3.4.8 – Jay Aug 09 '18 at 14:53
  • What happens if you change your `b` request to this: `b = request.args.get('basket', 'value')`? Does it output `value`? – CodeLikeBeaker Aug 09 '18 at 15:01
  • it takes value as default then, it's returning execvalue instead of execNone – Jay Aug 09 '18 at 15:20
  • Odd. It should just work then. I am unable to replicate with the exact code you've posted and with the same URL with args. Have you tried just `?basket=app` without the `apple` arg? Have you tried a different argument name? – CodeLikeBeaker Aug 09 '18 at 15:22
  • yeah, i don't understand what i am missing. with a single argument it works with second of any name it returns none – Jay Aug 09 '18 at 15:26
  • Please do a `print(request.args)` and let me know what the output is. It should be something like this: `ImmutableMultiDict([('basket', 'app'), ('apple', 'exec')])` – CodeLikeBeaker Aug 09 '18 at 15:29
  • it's just ImmutableMultiDict([('apple', 'exec')]) . – Jay Aug 09 '18 at 15:36
  • And your URL is exactly this: `http://127.0.0.1:5000/login?apple=exec&basket=app`? Are you typing in a different language? Perhaps an encoding issue in your URL? – CodeLikeBeaker Aug 09 '18 at 15:37
  • yeah it's exactly http://127.0.0.1:5000/login?apple=exec&basket=app . Encoding issue how can i find if it exists or remove it – Jay Aug 09 '18 at 15:40
  • Did you say this happens even if you were to change the values of exec and app? So, have you tried `apple=value1&basket=value2`? – CodeLikeBeaker Aug 09 '18 at 15:41
  • yeah true, apple=value1&basket=value2 also returns value1None – Jay Aug 09 '18 at 15:44
  • Might I suggest you reinstall flask? `pip uninstall flask` and the reinstall it. `pip install flask`. You may need pip3 depending on your environment. – CodeLikeBeaker Aug 09 '18 at 15:45
  • Also, see this post to see if this might clue you in on your issue: https://stackoverflow.com/questions/25065900/request-args-getkey-gives-null-flask – CodeLikeBeaker Aug 09 '18 at 15:53
  • 1
    uninstall didn't work, yeah sure will look into the link. thank you so much! – Jay Aug 09 '18 at 16:10
  • used webargs instead of requests to take parameters from URL and it worked anyways I think I did some mistake with using requests – Jay Aug 09 '18 at 18:20
  • Interesting. Not sure what mistake you did. Ever flask application that I've written has used request.args, or request.form to get the data. I'm glad you at least figured out something. – CodeLikeBeaker Aug 09 '18 at 18:23

1 Answers1

-1

There is no issue with the code in picking up the argument, actually the thing is you cant return 2 items to browser. It just cannot pick them up (even though python is capable of returning more than 1 values).

The below code is proof that the values are being picked up correctly.

from flask import Flask, request, render_template

app = Flask(__name__)


@app.route('/login', methods=['GET'])
def login():
    a = request.args.get('apple')
    b = request.args.get('basket')
    return a+b


if __name__ == "__main__":
    app.run()

Gives:

execapp
kernel
  • 49
  • 6
  • Your answer doesn't explain why he would get `None` for the `b` value. That appears to be the issue. Even though his code works as is, there has to be something he has (version perhaps) which is causing him to get `None` – CodeLikeBeaker Aug 09 '18 at 14:48
  • true i am getting value as execNone not execapp. How come you got execapp with same code. What version are you using and how do i make sure i am using the right one. – Jay Aug 09 '18 at 14:55
  • Btw request.args.get() retruns None if it is not able to get the value based on the key provided. Try updating your requests version – kernel Aug 09 '18 at 14:57
  • See this https://stackoverflow.com/questions/47071256/how-to-update-upgrade-a-package-using-pip – kernel Aug 09 '18 at 15:02
  • Btw I have removed the unnecessary typecasting, please try the above code now! – kernel Aug 09 '18 at 15:05
  • Even after updating requests i am getting None. Please let me know if i am missing anything else – Jay Aug 09 '18 at 15:17