-1
curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json
Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55
Jasmita Sagi
  • 47
  • 1
  • 7

1 Answers1

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
  • 1
    actually 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
  • 1
    yes 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