skip to Main Content

I am using this code to send data to the front end and then redirect it to another function.

@app.route('/get_destination_data', methods=['GET', 'POST'])
def get_destination_data():
    data = request.get_json()
    return jsonify({'redirect': url_for("show_destination_info", city_info=data)})

@app.route('/show_destination_info/<city_info>', methods=['GET', 'POST'])
def show_destination_info(city_info):
    return render_template('city-info.html', data=eval(city_info),
                           lists=UserLists.query.filter_by(user_id=current_user.id).all())

The second function is supposed to receive the date thru the url which gets converted to dictionary. This works perfectly fine in the local host. The data which I receive is a string that looks like this {'city_name': 'London', 'country_name': 'United Kingdom', 'id': 27456, 'state_prov': 'England'} and is easy to handle later.

In the server however I am getting this error message:

[2023-08-20 09:34:00,431] ERROR in app: Exception on /show_destination_info/%7B'city_name':%20'London',%20'country_name':%20'United%20Kingdom',%20'id':%2027456,%20'state_prov':%20'England'%7D [GET]
Traceback (most recent call last):
  File "/home/rabotataco/virtualenv/cgi-bin/www/3.9/lib/python3.9/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/rabotataco/virtualenv/cgi-bin/www/3.9/lib/python3.9/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/rabotataco/virtualenv/cgi-bin/www/3.9/lib/python3.9/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/rabotataco/virtualenv/cgi-bin/www/3.9/lib/python3.9/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/rabotataco/virtualenv/cgi-bin/www/3.9/lib/python3.9/site-packages/flask_login/utils.py", line 290, in decorated_view
    return current_app.ensure_sync(func)(*args, **kwargs)
  File "/home/rabotataco/cgi-bin/www/proj1/routes_user.py", line 175, in show_destination_info
    return render_template('user_city-info.html', data=eval(city_info),
  File "<string>", line 1
    %7B'city_name':%20'London',%20'country_name':%20'United%20Kingdom',%20'id':%2027456,%20'state_prov':%20'England'%7D
    ^
SyntaxError: invalid syntax

the next to the last line is actually the header which is same as the one in my localhost but for some reason same code in the server cant handle this type of data.

could somebody help me please? What would be the possible solution here?

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution that works for me. I am not sure if this is the right approach but it does trick. The problem is that URLs are not always decoded as expected in WSGI environment. this is why I'm getting the url as it is and slice only the part I need.

    @app.route('/get_destination_data', methods=['GET', 'POST'])
    def get_destination_data():
        wholeurl = request.url
        baseurl = request.base_url
        data = {'redirect': '/show_destination_info'+wholeurl[len(baseurl):]}
        return data
    
    
    @app.route('/show_destination_info', methods=['GET', 'POST'])
    def show_destination_info():
        data = request.values
        return render_template('city_info.html', data=data)
    

  2. If you are sending data as JSON then send it in the body of the request or even key value pairs in the URL parameters, not the URL path.

    Side note: You are using eval with data coming from user input. This is a massive security risk.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search