我试图用curl调用restapi。api端点在程序中动态生成,并上载一个json文件。在
with open('data.json','w') as f:
f.write(json.dumps(data))
cmd = 'curl -X PUT -H "Content-Type: application/json" -d @data.json {0}'.format(put_uri)
print cmd
p= subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p.wait()
output, errors = p.communicate()
if p.returncode != 0:
print "Request failed"
在这里,我正在打印生成的命令,当我从shell运行该命令时,它正在按预期的方式工作。但是在Popen运行相同的命令时会抛出一些json验证错误,这是错误的
^{pr2}$
这段代码不能确保在调用
curl
之前,JSON被完全写入磁盘。在在}跟在
f.flush()
后面使用f.flush()
或{f.write(json.dumps(data))
后面。在相关问题 更多 >
编程相关推荐