Replicate Curl command with Python Requests -
what syntax transforming following python requests call?
curl -f file=@/your/data.csv http://host/ingest i have tried following:
files = {'file': ('injestable_file', open(temp_file, 'rb'))} requests.post(url,files=files) but not work, corresponding curl command work.
you must not showing exact code because curl command provide , code provide works. see 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 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 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') fd:\n reg = re.compile(r'__version__ = [\\'\"]([^\\'\"]*)[\\'\"]')\n 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 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 interactive python shell:
>>> import pprint 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 setuptools import setup\nexcept importerror:\n 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 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 exception of unicode literals peeking through, code should work perfectly.
please edit post more details including version of requests you're using , far more details data you're trying post. can not when post correct.
Comments
Post a Comment