gpt4 book ai didi

python - 使用子进程调用时 Curl 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:35 27 4
gpt4 key购买 nike

我正在尝试使用curl 调用REST api。 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 验证错误,这很奇怪

{
"success" : false,
"message" : "Resource cannot be parsed due to Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)\n at [Source: java.io.StringReader@444c7495; line: 1, column: 2]"
}

最佳答案

此代码无法确保在调用 curl 之前将 JSON 完全写入磁盘。

f.write(json.dumps(data))之后使用f.flush()f.close() .

关于python - 使用子进程调用时 Curl 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com