我如何使用pycurl执行以下代码
How can i execute the following code using pycurl
curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json
如果您是编码新手,请不要使用 pycurl
它通常被认为已过时。而是使用可以与 pip install requests
.
一起安装的 requests
下面是与 requests
等效的方法:
import requests
with open('test.json') as data:
headers = {'Authorization': 'Bearer 26lhbngfsybdayabz6afrc6dcd'
'Content-Type' : 'application/json'}
r = requests.post('https://api.smartsheet.com/1.1/sheets', headers=headers, data=data)
print r.json
如果你一定要用pycurl
我建议你开始reading here。通常它会通过这个(未经测试的)代码完成:
import pycurl
with open('test.json') as json:
data = json.read()
c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://api.smartsheet.com/1.1/sheets')
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd',
'Content-Type: application/json'])
c.perform()
这表明 requests
更优雅。
curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json
如果您是编码新手,请不要使用 pycurl
它通常被认为已过时。而是使用可以与 pip install requests
.
requests
下面是与 requests
等效的方法:
import requests
with open('test.json') as data:
headers = {'Authorization': 'Bearer 26lhbngfsybdayabz6afrc6dcd'
'Content-Type' : 'application/json'}
r = requests.post('https://api.smartsheet.com/1.1/sheets', headers=headers, data=data)
print r.json
如果你一定要用pycurl
我建议你开始reading here。通常它会通过这个(未经测试的)代码完成:
import pycurl
with open('test.json') as json:
data = json.read()
c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://api.smartsheet.com/1.1/sheets')
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd',
'Content-Type: application/json'])
c.perform()
这表明 requests
更优雅。