From a4b4046c2bafa507f0c1a4704f8f8d3442a04689 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 30 Jul 2016 23:31:49 +0300 Subject: Fix url encoding issue --- project/shoutboxapicommunicator.py | 18 ++++++++++++------ 1 file 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!") -- cgit v1.3