Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
KeyError urllib
#1
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
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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&param2=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 
Reply
#4
OK, Thk for you help.

I will try and come back to you
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  urllib can't find "parse" rjdegraff42 6 2,147 Jul-24-2023, 05:28 PM
Last Post: deanhystad
  Help with urllib.request Brian177 2 2,866 Apr-21-2021, 01:58 PM
Last Post: Brian177
  urllib.request ericmt123 2 2,430 Dec-21-2020, 06:53 PM
Last Post: Larz60+
  urllib is not a package traceback cc26 3 5,380 Aug-28-2020, 09:34 AM
Last Post: snippsat
  urllib request error 404 Coco 2 4,400 May-11-2019, 02:47 PM
Last Post: Larz60+
  URLLIB.REQUEST Not Working hallofriends 1 6,380 Sep-18-2017, 05:00 PM
Last Post: Larz60+
  how to loop data in urllib? pythonlover 4 7,725 Jan-18-2017, 06:53 PM
Last Post: pythonlover

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020