Royal
Royal

Reputation: 208

flask get argument with special character and white spaces in it using URL

I am using flask URL to get parameters from front to back to pass it to query and it is working great with simple strings without any special character or space in it. However, when I pass "Historic_data CBD_3 (10WAY_10.0mWx59.0mW:28)" , it is not working. (string with special character like () . : and with white space)

I am running it on Linux. I tested it on windows and it was fine.

front 
@app.route('/design/<string:des>')
callback
/design/Historic_Intersection%20CBD_3%20(3WAY_6.0mWx5.0mW:10) 

I think URL encoding is being problem here, any suggestions ?

Upvotes: 1

Views: 1781

Answers (2)

Puzzlemaster
Puzzlemaster

Reputation: 174

Instead using url pass you can pass it with $.ajax({data: data})

let data_to_back = "Historic_data CBD_3 (10WAY_10.0mWx59.0mW:28)"
$.ajax({
            type: 'GET',
            url: 'pass/design/',
            dataType: "json",
            data: data)}

Upvotes: 1

mCoding
mCoding

Reputation: 4869

See quote and unquote in urllib.parse. https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote

Basically, URLs cannot have certain characters in them (like spaces), so if your URL contains one of these bad characters, it gets replaced, which is what you observe. The quote function takes any string and gives you the URL version of it. The unquote takes the URL version and gives you back the original.

If you need to do the same thing in query parameters, use quote_plus and unquote_plus instead.

Upvotes: 2

Related Questions