当我在命令行中运行curl请求但在使用python时无法获得输出时,我得到输出

时间:2015-04-01 09:40:01

标签: python json curl

subprocess.call(' curl https://api.smartsheet.com/1.1/sheets -H"授权:Bearer 26lhbngfsybdayabz6afrc6dcd" -H"内容类型:application / json" -X POST - d @ test.json')

我的test.json有:

{  
   "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"
         ]
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

试试这个:

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

虽然您可以使用urllib2库,因为这是为了那种操作

使用urrlib2检查此示例并阅读响应http://www.2maomao.com/blog/python-http-post-a-binary-file-using-urllib2/