python requests api - http multipart post with file and form-data -
Use of Requests 2.4.3. I want to post a file and send some fields from one form to my flask server. I already have an angular app that confirms that the flask is working btw
this topic But on the basis of various threads I have not tried to get some benefit.
data = {'formfoo': 'whatever'} files = {'Test.csv': open (trappath, 'rb'), 'data': json.dumps (data) )} Resp = Super (session, auto) .post (url, files = files, verify = false, header = multipart) ## This error brings the ### file "../2.7/lib/python2.7/ Json / decoder.py ", value alière (" no JSON object can not be decoded ") in row 382, raw_decode
b) separate data and files from the post API data Pass = {'formfoo': 'whatever'} files = {'test.csv': open (fpath, 'rb')} resp = super (session, auto). Post (url, data = data, file = files, verify = false, headers = multipart)
## brings this error. Increase value error in "raw_decode" (a) ### file "../2/2/lib/python2.7/json/decoder.py", row 383 ("No JSON object can be decoded ")
c) Pass data and file separately for the post api. Just like normal word jsonDumps does the data dictionary: {= formfoo ':' whatever '} files = {' test.csv ': open (fpath,' rb ')} resp = super (session, self) .post Url, data = json.dumps (data), files = files, verify = false, header = multipurpose)
## This error brings the ### file "./venv/ Lib / python2.7 / site-packages / requests / models
/ html>
No way the form data is received by the server because customer encoding fails. Any suggestions?
< Code> Requests / 2.4.3 Works as expected:
#! / Usr / bin / env python Import request response = request.post ('http: // "Data data": data = {"data"}, data = {"file": files = {"file": open ("file", "rb")}, parameters = {'param': 'param data'}, }) Response.Res_Forrestast () print (response.json ())
request
post / post? Ultimate = ultimate + data host HTTP / 1.1 : Httpbin.org Content-Length: 238 Connection: Keep Accepted Alive: * / * Accept - Encoding: gzip, deflate users Class-agent: python-request / 2.4.3 Content-Type: multipart / form-data; Limit = 0f34a3cfc1b448e78ef2bef3176d8948 --0f34a3cfc1b448e78ef2bef3176d8948 Theme: Form-data; Name = "data" data data --0f34a3cfc1b448e78ef2bef3176d8948 content-presentation: form-data; Name = "file"; Filename = "file" data --0f34a3cfc1b448e78ef2bef3176d8948--
output
{'files': {' file ':' data '},' form ':' 'Data': 'data data'}, 'url': 'http://httpbin.org/post?param=param+data', 'args': {'param': 'ultimate data'}, 'Data': '', 'header': {'content-length': '238', 'connection': 'off', 'host': 'httpbin.org', 'user-agent': 'python-request / 2.4.3 ',' X-request-id ':' 7afbf052-98f0-4e5e-8c6f-b16cfbfacbe9 ',' content-type ':' multipart / form-data;
Comments
Post a Comment