0

I am building a web crawler app on Google App Engine. I want to use a post method to pass the variable to Flask. Then, the received variable became the input of my web crawler app. However, Flask only accept single variable from post. If I add another variable in the funciton, Flask would crash.

I have limited knowledge in Flask and Google app engine. I struggled with the problem for several days and your help will be highly appreciated.

Failed function

#server-side function that does not work,with 2 variable passed
@app.route('/bac',methods=['GET', 'POST'])
def bac():
    request_json = request.get_json()
    filename = request_json["filename"]
    url = request_json["url"]
    #baseconnect.Baseconnect(url=url,filename=filename).run()
    return filename,url

#The function to post on client side

import requests
req = requests.Session()
data = req.post('https://project.appspot.com/bac',json={"filename":"yuan","url":"https:...f5"})
print(data.text)

#output:
 Internal server eror 500

Succeeded function

#server-side function that works,with 1 variable passed
@app.route('/bac',methods=['GET', 'POST'])
def bac():
    request_json = request.get_json()
    filename = request_json["filename"]
    #url = request_json["url"]
    #baseconnect.Baseconnect(url=url,filename=filename).run()
    return filename

#The function to post on client side

import requests
req = requests.Session()
data = req.post('https://project.appspot.com/bac',json={"filename":"yuan"})
print(data.text)
#output:
 yuan

Flask seems only accept single variable. What is the problem....

Yuan
  • 462
  • 4
  • 12
  • Is `filename` actually defined? – TsungJui Wang Jul 23 '19 at 15:33
  • yes, when the crawling task finished, the output will use the filename. – Yuan Jul 23 '19 at 15:35
  • That's weird. As far as I know, `request.args.get` only gets the query string in URL, which is not specified here. – TsungJui Wang Jul 23 '19 at 15:37
  • Can you try to print `filename` inside the route after `request.args.get()`? – TsungJui Wang Jul 23 '19 at 15:47
  • And also, route url should start with a '/'. Change `route` to `'/route'`. – TsungJui Wang Jul 23 '19 at 15:51
  • Hello, Wang, I was testing the function in the previous hour and correct several error that you pointed out. Thank you very much! Now, I realized that the problem is that Flask could only accept single variable. If i set two variable then it will response internal server error 500. It is quite weried. – Yuan Jul 23 '19 at 17:04
  • [this link might help](https://stackoverflow.com/questions/15182696/multiple-parameters-in-in-flask-approute) – RNHTTR Jul 23 '19 at 17:14
  • 1
    @RNHTTR I want to say THANK YOU~~. That is the problem. I did not know flask had the difference with a normal Python function. You helped me a lot~ – Yuan Jul 24 '19 at 02:37
  • np :) @GMarsh did all the hard work in his response to the question i linked. – RNHTTR Aug 15 '19 at 14:12

1 Answers1

2

The problem you have here is that Flask only returns Response object, and Flask will consider return filename, url a shortcut of return Response, status or header.

In this case, url becomes the http status code or header, which is obviously not right.

You need flask.jsonify() to return the proper format of so called 'multiple variables'.

Something like this: (only the important part)

# In server-side code
from flask import jsonify
@app.route('/bac',methods=['GET', 'POST'])
def bac():
    request_json = request.get_json()
    filename = request_json["filename"]
    url = request_json["url"]
    # Do your logic here
    return jsonify({ filename_returned: filename, url_returned: url })


# client-side
import requests
req = requests.Session()
json_data = req.post('https://project.appspot.com/bac',json={"filename":"yuan", "url": "http:xxxxxxx"})
real_data = json.loads(json_data)
# real_data should be the result you want
TsungJui Wang
  • 328
  • 1
  • 4
  • Thanks for your kind instruction. It did help me solve the problem. I am so happy that I could run my app properly! – Yuan Jul 24 '19 at 02:34