将以下内容转换为 Python 请求调用的语法是什么?
curl -F file=@/your/data.csv http://host/ingest
我尝试了以下方法:
files = {'file': ('injestable_file', open(temp_file, 'rb'))}
requests.post(url,files=files)
但这不起作用,但相应的 curl 命令确实有效。
将以下内容转换为 Python 请求调用的语法是什么?
curl -F file=@/your/data.csv http://host/ingest
我尝试了以下方法:
files = {'file': ('injestable_file', open(temp_file, 'rb'))}
requests.post(url,files=files)
但这不起作用,但相应的 curl 命令确实有效。
You must not be showing your exact code because the curl command you provide and the code you provide works. See my output:
~ curl -F foo=bar -F file=@setup.py https://httpbin.org/post
{
"headers": {
"Accept": "*/*",
"Content-Length": "2370",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.29.0",
"Content-Type": "multipart/form-data; boundary=----------------------------959364026805"
},
"files": {
"file": "#!/usr/bin/env python\n\nimport sys\nimport os\nimport re\n\nkwargs = {}\nrequires = []\npackages = [\n \"github3\",\n \"github3.gists\",\n \"github3.repos\",\n \"github3.issues\",\n]\n\ntry:\n from setuptools import setup\n kwargs['test_suite'] = 'run_tests.collect_tests'\n kwargs['tests_require'] = ['mock', 'expecter', 'coverage==3.5.2']\n packages.append('tests')\nexcept ImportError:\n from distutils.core import setup # NOQA\n\nif sys.argv[-1] in (\"submit\", \"publish\"):\n os.system(\"python setup.py sdist upload\")\n sys.exit()\n\nrequires.extend([\"requests >= 1.2.3\", \"uritemplate.py >= 0.2.0\"])\n\n__version__ = ''\nwith open('github3/__init__.py', 'r') as fd:\n reg = re.compile(r'__version__ = [\\'\"]([^\\'\"]*)[\\'\"]')\n for line in fd:\n m = reg.match(line)\n if m:\n __version__ = m.group(1)\n break\n\nif not __version__:\n raise RuntimeError('Cannot find version information')\n\nsetup(\n name=\"github3.py\",\n version=__version__,\n description=(\"Python wrapper for the GitHub API\"\n \"(http://developer.github.com/v3)\"),\n long_description=\"\\n\\n\".join([open(\"README.rst\").read(),\n open(\"HISTORY.rst\").read()]),\n license=open('LICENSE').read(),\n author=\"Ian Cordasco\",\n author_email=\"graffatcolmingov@gmail.com\",\n url=\"https://github3py.readthedocs.org\",\n packages=packages,\n package_data={'': ['LICENSE', 'AUTHORS.rst']},\n include_package_data=True,\n install_requires=requires,\n classifiers=[\n 'Development Status :: 5 - Production/Stable',\n 'License :: OSI Approved',\n 'Intended Audience :: Developers',\n 'Programming Language :: Python',\n 'Programming Language :: Python :: 2',\n 'Programming Language :: Python :: 2.6',\n 'Programming Language :: Python :: 2.7',\n 'Programming Language :: Python :: 3',\n 'Programming Language :: Python :: 3.2',\n 'Programming Language :: Python :: 3.3',\n 'Programming Language :: Python :: Implementation :: CPython',\n ],\n **kwargs\n)\n"
},
"origin": "...",
"form": {
"foo": "bar"
},
"url": "http://httpbin.org/post",
"data": "",
"args": {},
"json": null
And from the interactive python shell:
>>> import pprint as pp
>>> import requests
>>> pp.pprint(requests.post('https://httpbin.org/post', files={'file': ('filename', fd)}).json())
{u'args': {},
u'data': u'',
u'files': {u'file': u"#!/usr/bin/env python\n\nimport os\nimport sys\n\nimport requests\n\ntry:\n from setuptools import setup\nexcept ImportError:\n from distutils.core import setup\n\nif sys.argv[-1] == 'publish':\n os.system('python setup.py sdist upload')\n sys.exit()\n\npackages = [\n 'requests',\n 'requests.packages',\n 'requests.packages.charade',\n 'requests.packages.urllib3',\n 'requests.packages.urllib3.packages',\n 'requests.packages.urllib3.contrib',\n 'requests.packages.urllib3.packages.ssl_match_hostname'\n]\n\nrequires = []\n\nsetup(\n name='requests',\n version=requests.__version__,\n description='Python HTTP for Humans.',\n long_description=open('README.rst').read() + '\\n\\n' +\n open('HISTORY.rst').read(),\n author='Kenneth Reitz',\n author_email='me@kennethreitz.com',\n url='http://python-requests.org',\n packages=packages,\n package_data={'': ['LICENSE', 'NOTICE'], 'requests': ['*.pem']},\n package_dir={'requests': 'requests'},\n include_package_data=True,\n install_requires=requires,\n license=open('LICENSE').read(),\n zip_safe=False,\n classifiers=(\n 'Development Status :: 5 - Production/Stable',\n 'Intended Audience :: Developers',\n 'Natural Language :: English',\n 'License :: OSI Approved :: Apache Software License',\n 'Programming Language :: Python',\n 'Programming Language :: Python :: 2.6',\n 'Programming Language :: Python :: 2.7',\n 'Programming Language :: Python :: 3',\n 'Programming Language :: Python :: 3.3',\n\n ),\n)\n"},
u'form': {},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate, compress',
u'Connection': u'close',
u'Content-Length': u'1748',
u'Content-Type': u'multipart/form-data; boundary=d76a42fcaca84b3288ea4fa177bdad60',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/1.2.3 CPython/2.7.3 Linux/3.2.29'},
u'json': None,
u'origin': u'...',
u'url': u'http://httpbin.org/post'}
With the exception of the unicode literals peeking through, your code should work perfectly.
Please edit your post with more details including which version of requests you're using and far more details about the data you're trying to post. We can not help you when what you post is exactly correct.