![]() |
KeyError urllib - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: KeyError urllib (/thread-14229.html) |
KeyError urllib - DavidFernandez - Nov-20-2018 Hello, I'm trying to run a command by url and I get the following error code: KeyError: "text" Here is my python function with web.urlopen ('http://{0}:{1}/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version={2}&token={3}&payload={"text":"Test message.\nDavid fait un test API"}'.format(self.ip,self.port,self.apiversion,self.token)) as url:Here is the error message I receive: with web.urlopen ('http://{0}:{1}/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version={2}&token={3}&payload={"text":"Test message.\nDavid fait un test API"}'.format(self.ip,self.port,self.apiversion,self.token)) as url: KeyError: "text" Yet when I take the url in a web browser it works: http://*******:*****/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token="***************"&payload={"text":"Test message.\nDavid fait un test API"} Can you tell me why python considers that the "text" key is in error and how I can solve this problem. Thank you RE: KeyError urllib - ichabod801 - Nov-20-2018 Do you want that text bit to be part of the text formatting? If so, you need a text= parameter in the format method call. If not, you need to double the braces around that section starting with "text" ({{ and }}, this escapes them in terms of the format method call). RE: KeyError urllib - snippsat - Nov-20-2018 (Nov-20-2018, 09:01 PM)DavidFernandez Wrote: Can you tell me why python considers that the "text" key is in error and how I can solve this problem.Should not be using urllib,but Requests. Send payload in Post,will not work with a dictionary in a url address. If doing all in a url address have to be in format as param1=value1¶m2=value2 .Example: >>> import requests >>> >>> ip = '111.222.333' >>> port = 20 >>> apiversion = 99 >>> payload = dict(ip=ip, port=port, apiversion=apiversion) >>> payload {'apiversion': 99, 'ip': '111.222.333', 'port': 20} >>> r = requests.post('http://httpbin.org/post', json=payload) >>> r.status_code 200 >>> r.json() {'args': {}, 'data': '{"ip": "111.222.333", "port": 20, "apiversion": 99}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '51', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.19.1'}, 'json': {'apiversion': 99, 'ip': '111.222.333', 'port': 20}, 'origin': '83.143.86.74',[]} 'url': 'http://httpbin.org/post'} >>> r.json()['json']['port'] 20 RE: KeyError urllib - DavidFernandez - Nov-21-2018 OK, Thk for you help. I will try and come back to you RE: KeyError urllib - DavidFernandez - Nov-21-2018 Hi snippsat, This code is not work. My URL link remains unique until payload http://"IP":"PORT"/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token="TOKENKEY"&payload={"text":"Test message.\nDavid fait un test API"} I want to make dynamic only the values behind "text": Can you tell me how I can do to use it with urllib and json. Thank you |