Error in decoding in request

0

I am sending the second string via post (JavaScript) to my bootle server (Python):

query = {
'wiki': 'Tr%E1%BA%A7n_H%C6%B0ng_%C4%90%E1%BA%A1o',
'uid':'f17afd66aae3a'
}

$.post(url, query, function(data, status){}):

But when I print what I get from the post it gives the following result:

b'uid=f17afd66aae3a&wiki=Tr%25E1%25BA%25A7n_H%25C6%25B0ng_%25C4%2590%25E1%25BA%25A1o' - First print

In the case I make two split transforming this binary string into a dictionary through the function translate(postdata) .

{'uid': 'f17afd66aae3a', 'wiki': 'Tr%25E1%25BA%25A7n_H%25C6%25B0ng_%25C4%2590%25E1%25BA%25A1o'} - Second print

And as my code will access the wikipedia article with such name:

https://en.wikipedia.org/wiki/Tr%E1%BA%A7n_H%C6%B0ng_%C4%90%E1%BA%A1o

I can not access it because my request has the following url:

https://en.wikipedia.org/wiki/Tr%25E1%25BA%25A7n_H%25C6%25B0ng_%25C4%2590%25E1%25BA%25A1o

That generates an error because the page does not exist.

There's my code in the bottle:

@post('/update_article')                                                        
@enable_cors                                                                    
def update_article_post():                                       
    token = request.cookies.get('token', '0')                                   
    if load_token(token):                                                       
        postdata = request.body.read()
        print(postdata)                                        
        dici = translate(postdata)                                              
        print(dici)                                                             
       # res = update_article(dici['uid'], dici['wiki'])                        
       # return {'data': res}                                                   
    else:                                                                       
         return redirect('/login.html')

Is there any way to get the post the right way, or code this string?

    
asked by anonymous 05.09.2018 / 19:36

2 answers

1

According to the Bottle documentation you can access parameters received from a POST using request.forms.get('campo') .

So your question looks like it can be solved with the code:

wiki = request.forms.get('wiki')

If the Bottle does not auto-decode the string you can use the urlli.parse.unquote() .

from urllib.parse import unquote

print(unquote('Tr%25E1%25BA%25A7n_H%25C6%25B0ng_%25C4%2590%25E1%25BA%25A1o'))
# Saída: Trần_Hưng_Đạo
    
05.09.2018 / 20:27
1

try base64 encoding

In the Python server try to decode the string before playing on the wiki, use the Python b64decode

import base64
Request = 'cmVzcG9zdGEgZGVjb2RpZmljYWRhIA==' 
resposta = base64.b64decode(Request)
print(resposta)

the output will be

resposta decodificada 
    
05.09.2018 / 20:22