diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2016-07-30 23:31:49 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2016-07-30 23:31:49 +0300 |
| commit | a4b4046c2bafa507f0c1a4704f8f8d3442a04689 (patch) | |
| tree | 61247742fada724c89cf5a6c7a68a9803809d0f8 | |
| parent | 006b055d259e70ae023dd0bf86897c910e2d70fd (diff) | |
Fix url encoding issue
| -rw-r--r-- | project/shoutboxapicommunicator.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/project/shoutboxapicommunicator.py b/project/shoutboxapicommunicator.py index 7af430e..33253ac 100644 --- a/project/shoutboxapicommunicator.py +++ b/project/shoutboxapicommunicator.py @@ -1,4 +1,5 @@ import json +from urllib.parse import quote import logging import requests @@ -48,14 +49,19 @@ class ShoutboxCommunicator(BaseCommunicator): @staticmethod def send(data): """Send a message to the API""" - post_params = "?user={}&text={}&ip={}×tamp={}&api_token={}".format( - data["user"], data["text"], data["ip"], data["timestamp"], - ShoutboxCommunicator.token - ) + fields = ("user", "text", "ip", "timestamp", "api_token") + post_params = "?" + for field in fields: + if field != fields[-1]: + post_params = "{}{}={}".format(post_params, field, quote(str(data[field]).encode("utf-8"))) + post_params += "&" + else: + post_params = "{}{}={}".format(post_params, field, ShoutboxCommunicator.token) + url = ShoutboxCommunicator.post_url() + post_params try: - requests.post(ShoutboxCommunicator.post_url() + post_params, "") + requests.post(url, "") user = data["user"] - logging.info("Sent message from {} to shoutbox.".format(user)) + logging.info("Sent message from {} to shoutbox, with url:\n{}".format(user, url)) except: logging.warning("Failed to send message to shoutbox API!") |
