我正在尝试实施推送通知。我可以使用python:
通过此调用触发通知curl -X POST -H "Content-Type: application/json" -H "X-Thunder-Secret-Key: secret2" --data-ascii "\"Hello World\"" http://localhost:8001/api/1.0.0/key2/channels/mychannel/
从命令行可以正常工作。
首先我尝试使用子进程,但它给了我这个奇怪的错误:
curl: (1) Protocol "http not supported or disabled in libcurl
所以我放弃了,我试图使用pycurl。但问题是我不知道如何处理-X和--data-ascii选项。
import pycurl
c = pycurl.Curl()
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','X-Thunder-Secret-Key: secret2'])
c.setopt(c.URL, 'http://localhost:8001/api/1.0.0/key2/channels/mychannel/')
c.perform()
print("Done")
那么如何添加-X选项以及如何使用请求发送文本消息?
答案 0 :(得分:2)
如果您需要HTTP POST请求,see documentation example。
我认为这样的事情应该有效(我已经使用过python 2):
import pycurl
c = pycurl.Curl()
postfields = '"Hello World"'
c.setopt(c.URL, 'http://pycurl.sourceforge.net/tests/testpostvars.php')
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','X-Thunder-Secret-Key: secret2'])
# Here we set data for POST request
c.setopt(c.POSTFIELDS, postfields)
c.perform()
c.close()
此代码生成以下HTTP数据包:
POST /tests/testpostvars.php HTTP/1.1
User-Agent: PycURL/7.19.5.1 libcurl/7.37.1 SecureTransport zlib/1.2.5
Host: pycurl.sourceforge.net
Accept: */*
Content-Type: application/json
X-Thunder-Secret-Key: secret2
Content-Length: 13
"Hello World"