Jasmita Sagi
Jasmita Sagi

Reputation: 47

I get the output when i run the curl request in command line but not able to get the output when using python

subprocess.call('curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json' )

My test.json has:

{  
   "name":"newsheet",
   "columns":[  
      {  
         "title":"Favorite",
         "type":"CHECKBOX",
         "symbol":"STAR"
      },
      {  
         "title":"Primary Column",
         "primary":true,
         "type":"TEXT_NUMBER"
      },
      {  
         "title":"Status",
         "type":"PICKLIST",
         "options":[  
            "Not Started",
            "Started",
            "Completed"
         ]
      }
   ]
}

Upvotes: 0

Views: 302

Answers (1)

lapinkoira
lapinkoira

Reputation: 8988

Try this:

from subprocess import PIPE, Popen

p = Popen('curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: application/json" -X POST -d @test.json', stdin=PIPE, stdout=PIPE)

out, err = p.communicate()
print "Output %s" % out
print "Error %s" % err

Although you could use urllib2 library because is made for those kind of operation

Check this example using urrlib2 and reading the response http://www.2maomao.com/blog/python-http-post-a-binary-file-using-urllib2/

Upvotes: 1

Related Questions