1

What is the syntax for transforming the following into a Python requests call?

curl -F file=@/your/data.csv http://host/ingest

I have tried the following:

 files = {'file': ('injestable_file', open(temp_file, 'rb'))}
 requests.post(url,files=files)

but this does not work, but the corresponding curl command does work.

4
  • That question refers to a get where this is clearly a post. Not a duplicate. Commented Sep 4, 2013 at 16:11
  • it was not clear that it was a post ... now it is(post edit) ... what does "this does not work" mean? did you get an error? does temp_file exist? and open ok? if you created temp_file with the tempfile module there is a strong chance that it is deleted automagically. the code you posted should work fine. assumming the url is good and the file is good and the url accepts posts with that name as the files variable Commented Sep 4, 2013 at 16:30
  • Temp files exists. I did not create with any of the python temporary file/directory functionality. It will continue to exists even after closed. Commented Sep 4, 2013 at 16:36
  • can you post your actual code including the url ? Commented Sep 4, 2013 at 17:13

1 Answer 1

2

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 [email protected] 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=\"[email protected]\",\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='[email protected]',\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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had an error elsewhere in my code. Marking you as the answer since t is a thorough answer and correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.