Reputation: 87
I'm new to Flask and encoding. Using Python3 I have an app:
from flask import Flask
from flask import request
from flask import jsonify
import requests
app = Flask(__name__)
QUERY_PARAM = 'q'
@app.route('/response', methods=['GET'])
def get_query():
query = request.args.get(QUERY_PARAM)
user_input = dict()
user_input['text'] = query
return jsonify(user_input), requests.codes.ok
I put in /response?q=what is ab\u0026c
It is now returning: {"text":"what is ab\u0026c"}
I want it to return: {"text":"what is ab&c"}
Tried a couple of encoding in the code but none of them worked for me. Can someone tell me how I can achieve this and help me understand it?
Upvotes: 0
Views: 757
Reputation: 141
You implementation seems to be working correctly, but you are passing the query param in a wrong way.
You want to send the text: "what is ab&c"
.
This text needs to be encoded to encoded for urls.
For example you could use python for this, by running the following lines in the shell:
>>> from urllib.parse import urlencode
>>> payload = dict(q="what is ab&c")
>>> urlencode(payload)
'q=what+is+ab%26c'
With that result we know that the request would be:
http://localhost:5000/response?q=what+is+ab%26c
Which results in:
{
"text": "what is ab&c"
}
Upvotes: 2