【问题标题】:I get the output when i run the curl request in command line but not able to get the output when using python当我在命令行中运行 curl 请求时得到输出,但在使用 python 时无法得到输出
【发布时间】:2015-04-01 09:40:01
【问题描述】:

subprocess.call('curl https://api.smartsheet.com/1.1/sheets -H "Authorization: Bearer 26lhbngfsybdayabz6afrc6dcd" -H "Content-Type: 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"
         ]
      }
   ]
}

【问题讨论】:

    标签: python json curl


    【解决方案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/

    【讨论】:

    • 它应该在 smartsheet 中创建一个新工作表。但是上面的代码没有任何反应:(
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2017-08-30
    相关资源
    最近更新 更多