将curl命令与表单文件转换为Python请求

huangapple go评论72阅读模式
英文:

Convert curl command with form files into python requests

问题

Here's the translation of the provided text:

我有以下的curl命令:

curl -X POST "_my_username_:_my_password_@10.2.25.209:5601/api/saved_objects/_import" -H "kbn-xsrf: true" --form file=@V:kibana\IndexPatterns\events.ndjson

这个命令完美地工作(将索引模式导入到Elasticsearch),但我试图将其转换为Python的requests请求。我尝试了几种方法,包括以下方式:

files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
                    headers={'kbn-xsrf': 'true'}, data=files)
files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
                    headers={'kbn-xsrf': 'true', 'Content-Type': 'text/plain'}, files=files)

不同的组合,包括使用或不使用@,将files参数作为字符串而不是字典等。我一直收到错误的请求和无效的内容类型错误(例如:{'message': 'Unsupported Media Type', 'error': 'Unsupported Media Type', 'statusCode': 415})。

请注意,有一些工具可以将curl转换为requests请求,但我尝试的所有工具都不识别files参数,要么忽略它,要么抛出异常。然而,命令本身是有效的。

我在这里做错了什么?

英文:

I have the following curl command:

curl -X POST "_my_username_:_my_password_@10.2.25.209:5601/api/saved_objects/_import" -H "kbn-xsrf: true" --form file=@V:kibana\IndexPatterns\events.ndjson

Which works perfectly (import index pattern into elasticsearch), but I'm trying to convert it to Python requests. I tried several ways, including the following:

files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
                    headers={'kbn-xsrf': 'true'}, data=files)


files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
                    headers={'kbn-xsrf': 'true', 'Content-Type': 'text/plain'}, files=files)

With different combinations of with or without the @, files as a single string instead of dictionary, ect. I keep getting errors on bad requests and invalid content types (for example: {'message': 'Unsupported Media Type', 'error': 'Unsupported Media Type', 'statusCode': 415}).

Note that there are some tools to convert curl to requests, but all the ones I tried don't recognize the files param, either ignoring it or throwing exception. The command itself, however, works.

What am I doing wrong here?

答案1

得分: 1

尝试以下操作:

import requests
from requests.auth import HTTPBasicAuth

用户名 = '_my_username_'
密码 = '_my_password_'
标头 = {'kbn-xsrf': 'true'}
上传URL = "http://10.2.25.209:5601/api/saved_objects/_import"
文件 = {'file': open('V:\algotec\analytics\install\Kibana\IndexPatterns\events.ndjson', 'rb')}

r = requests.post(上传URL, headers=标头, auth=HTTPBasicAuth(用户名, 密码), files=文件)
print(r.status_code)

如果您收到以下错误的坏请求:

错误: 'Bad Request',消息: '请求必须包含 kbn-xsrf
头。'

请根据以下内容修改标头信息并重试:

标头 = {
  'Content-Type': 'application/x-ndjson',
  'kbn-xsrf': 'anything',
  'Accept': 'application/x-ndjson'
}
英文:

Try the following:

import requests
from requests.auth import HTTPBasicAuth
    
username = '_my_username_'
password = '_my_password_'
headers = {'kbn-xsrf': 'true'}
upload_url = "http://10.2.25.209:5601/api/saved_objects/_import"
files = {'file': open('V:\algotec\analytics\install\Kibana\IndexPatterns\events.ndjson', 'rb')}

r = requests.post(upload_url, headers=headers, auth=HTTPBasicAuth(username, password), files=files)
print(r.status_code)

If you receive a bad request with this error

> error: 'Bad Request', message: 'Request must contain a kbn-xsrf
> header.'

Modify the header information as per the below and re-try.

headers = {
  'Content-Type': 'application/x-ndjson',
  'kbn-xsrf': 'anything',
  'Accept': 'application/x-ndjson'
}

huangapple
  • 本文由 发表于 2020年1月6日 23:45:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定