curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json
Asked
Active
Viewed 5,264 times
-1
Joe Kennedy
- 9,365
- 7
- 41
- 55
Jasmita Sagi
- 47
- 1
- 7
-
i am very new to coding, so i dont even know how to start :\ – Jasmita Sagi Apr 01 '15 at 12:00
-
1Start here: https://duckduckgo.com/?q=pycurl+tutorial – Jean-François Corbett Apr 01 '15 at 12:07
-
any syntax suggestions? – Jasmita Sagi Apr 01 '15 at 12:42
1 Answers
3
If you are new to coding, then don't use pycurl it is generally considered obsolete. Instead use requests which can be installed with pip install requests.
Here is how to do the equivalent with 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
If you must use pycurl I suggest that you start reading here. Generally it would be done by this (untested) code:
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()
This shows that requests is far more elegant.
mhawke
- 84,695
- 9
- 117
- 138
-
1actually it's not really obsolete. pycurl is also much faster: https://stackoverflow.com/a/32899936/1515686 – Tom-Oliver Heidel Dec 10 '17 at 22:34
-
1@Tom-OliverHeidel: That's interesting. By _generally obselete_ I mean that there are some specialist cases where it might be used, but in the general case, for a beginner, `requests` is so much better (and faster) to work with that I would not recommend `pycurl` unless there is a demonstrable need to use it. – mhawke Dec 10 '17 at 22:57
-
1yes you are absolutely right. I actually prefer requests over pycurl. Also pycurl could need some better documentation to be honest. – Tom-Oliver Heidel Dec 11 '17 at 03:46