我需要将通知发送到端点(配置了websockets)。此通知应从Python 3发送:
POST /myproject/notification/{nId}
我找到了一些如何发送Json数据的示例,但我只需要发送通知id
nId
import urllib.request
import json
data = {'ids': [1, 2, 3]}
myurl = "localhost"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
jsondataasbytes = jsondata.encode('utf-8')
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
我怎么能为你做同样的事
POST /myproject/notification/{nId}
我试过这个代码,但失败了。
data = 1
myurl = "localhost/myproject/notification/"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
我想用
requests
urllib
.
r = requests.post("localhost/myproject/notification/", data=1)
print(r.status_code, r.reason)