python curl 无法在 header 中 PUT 空内容类型
python curl unable to PUT empty content type in header
尝试使用 curl 和 python 子进程执行 PUT,但是,我无法为我的请求设置内容类型。
import subprocess
item = '{"title": "Copy", "id": "1mglMSA_wU", "type": "document", "parentId": "1WtlhD7a", "modifiedTime": "2019-01-16T21:33:36.631Z", "createdTime": "2019-01-16T21:29:26.327Z", "shared": "True"}'
cmd = f'curl -H "Content-Type: application/json" -X PUT localhost:9200/ax/_doc/1?pretty -d\'{item}\''
res = subprocess.Popen(f'ssh {user}@{host} {cmd}', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(res)
我不断收到以下错误:
{
"error" : "Content-Type header [] is not supported",
"status" : 406
}
远程 ssh
命令调用另一个 shell,因此您需要转义字符串两次,或者 - 更好 - 如果可以的话,去掉一个 shell。
此外,您正在重新发明 subprocess.run()
,但存在一些错误。
import subprocess
item = '{"title": "Copy", "id": "1mglMSA_wU", "type": "document", "parentId": "1WtlhD7a", "modifiedTime": "2019-01-16T21:33:36.631Z", "createdTime": "2019-01-16T21:29:26.327Z", "shared": "True"}'
res = subprocess.run(
['ssh', f'{user}@{host}',
'curl', '-H', 'Content-Type: application/json',
'-X', 'PUT', 'localhost:9200/ax/_doc/1?pretty', '-d', item],
capture_output=True, text=True, check=True) # no shell=True
print(res.stdout)
我看不出有什么特别的理由将命令放在一个单独的变量中,不过如果你愿意,你当然可以把它放在一个列表中,然后在将它传递给 subprocess.run()
.
或许还可以看看 Actual meaning of 'shell=True' in subprocess
直接的问题是你只是通过了 -H
Content-Type:
因为一层引号被剥掉了。
尝试使用 curl 和 python 子进程执行 PUT,但是,我无法为我的请求设置内容类型。
import subprocess
item = '{"title": "Copy", "id": "1mglMSA_wU", "type": "document", "parentId": "1WtlhD7a", "modifiedTime": "2019-01-16T21:33:36.631Z", "createdTime": "2019-01-16T21:29:26.327Z", "shared": "True"}'
cmd = f'curl -H "Content-Type: application/json" -X PUT localhost:9200/ax/_doc/1?pretty -d\'{item}\''
res = subprocess.Popen(f'ssh {user}@{host} {cmd}', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(res)
我不断收到以下错误:
{
"error" : "Content-Type header [] is not supported",
"status" : 406
}
远程 ssh
命令调用另一个 shell,因此您需要转义字符串两次,或者 - 更好 - 如果可以的话,去掉一个 shell。
此外,您正在重新发明 subprocess.run()
,但存在一些错误。
import subprocess
item = '{"title": "Copy", "id": "1mglMSA_wU", "type": "document", "parentId": "1WtlhD7a", "modifiedTime": "2019-01-16T21:33:36.631Z", "createdTime": "2019-01-16T21:29:26.327Z", "shared": "True"}'
res = subprocess.run(
['ssh', f'{user}@{host}',
'curl', '-H', 'Content-Type: application/json',
'-X', 'PUT', 'localhost:9200/ax/_doc/1?pretty', '-d', item],
capture_output=True, text=True, check=True) # no shell=True
print(res.stdout)
我看不出有什么特别的理由将命令放在一个单独的变量中,不过如果你愿意,你当然可以把它放在一个列表中,然后在将它传递给 subprocess.run()
.
或许还可以看看 Actual meaning of 'shell=True' in subprocess
直接的问题是你只是通过了 -H
Content-Type:
因为一层引号被剥掉了。