I coded a mock server, after I got a POST message with XML file, I need send back a response message with XML file also.
I found some example as below to send back response with XML content. But I think it's not a file. Anyone has idea on sending back response with a file?
result = open('output.xml', 'r').read()
r = Response(response=result, status=200, mimetype="application/xml")
Related
;TLDR
I want to send a file with requests.send() using multipart/form-data request without storing the file on a hard drive. Basically, I'm looking for an alternative for open() function for bytes object
Hello, I'm currently trying to send multipart/form-data request and pass in-memory files in it, but I can't figure out how to do that.
My app receives images from one source and sends them to another. Currently it sends get request directly to file, (e.g. requests.get('https://service.com/test.jpeg')), reads image's bytes and writes them into new file on the hard drive. The sending code that works looks like this:
def send_file(path_to_image: str)
url = get_upload_link()
data = {'photo': open(path_to_image, 'rb')}
r = requests.post(url, files=data)
send_file("test.jpeg")
The main issue I have with this approach is that I have to keep files on my hard drive. Sure, I can use my drive as some sort of a "temporary buffer" and just delete them after I no longer need these files, but I believe there's much more simple way to do that.
I want my function to receive bytes object and then send it. I actually tried doing that, but the backend doesn't accept them. Here's what I tried to do
Attempt 1
def send_file(image: bytes)
url = get_upload_link()
data = {'photo': open(image, 'rb')}
r = requests.post(url, files=data)
I get "ValueError: embedded null byte"
Attempt 2
def upload_photo(image: bytes):
url = get_upload_link()
file = BytesIO(image)
data = {'photo': file}
r = requests.post(url, files=data)
Backend server doesn't process my files correctly. It's like passing files=None, same response
I also tried:
sending the returning value of the methods: file.getbuffer() and file.read()
file.write(image) and then sending file
StringsIO object
etc.
Final notes
I noticed, that open() returns _io.BufferedReader object. I also looked for a way to construct its instance, but couldn't fund a way. Can someone help me, please?
UPD:
If anyone is interested, the receiving api is this
From the official documentation:
POST a Multipart-Encoded File
...
If you want, you can send strings to
be received as files:
url = 'https://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
I am building an application where data is sent to a server, the server creates an xlsx (excel) file with that data and returns that file to the client where at the end I want it to be displayed
Im using flask and the creation of the file itself with the data from the client works and the file is saved locally in the same folder. I tried several things but I cant seem to check wether the file was sent back correctly because I dont exactly know how to work with it on the client side. Currently I try sending the file back as following:
return send_file("my_file.xlsx", as_attachment=True)
and I also tried
return send_file("absolute/path/to/my_file", as_attachment=True)
On the client side I also tried all kind of things and Im currently at
print(r.content)
which prints tons of characters, backslashes etc..
and where r is
r = requests.get('http://127.0.0.1:5000/', params = {...})
So two problems:
I dont know if the file is correctly sent from the server, how can I check?
Probably answers the first one: How can I display or save the file on the client side?
The file is created with xlsxwriter and I dont get any error messages. Return status also is 200 so I guess my problem is opening the file on the client side. But if anybody has advice I would be really happy to hear!
Edit: File was sent correctly, the answer was:
r = requests.get('http://127.0.0.1:5000/', params = {...})
def save_xl(r):
with open('file.xlsx', 'wb') as f:
f.write(r.content)
save_xl(r)
And the file was create successfully
you can try saving the content of the request as a xlsx file.
r = requests.get('http://127.0.0.1:5000/', params = {...})
def save_xl(r):
with open('file.xlsx', 'wb') as f:
f.write(r.content)
save_xl(r)
I'm building a REST API, and it has to send a file in the response. I do not want to include the file content in the response body. Can we attach files to response ?
If I understood you right, you want to send a file with Content-Disposition header set to 'attachment'. Which instructs the browser to download/save the file, instead of displaying its contents inline on the page.
If that's what you want, then you'll have to do something like this:
from flask import make_response
#app.route('/txt')
def attachment():
resp = make_response('my text file')
resp.headers['Content-Type'] = 'text/plain;charset=UTF-8'
resp.headers['Content-Disposition'] = 'attachment;filename=SmartFileName.txt'
return resp
I'm trying to send .xml file via POST request:
files = {"FileName": open (path_to_file.xml, "rb")}
requests.post(url, files=files)
But file that sent from client side as
<protection>
<create name="domain1/domain2/>
<allowed name="domain1"/>
<allowed name="domain2"/>
</protection>
Received on server side as
<protection>
<create name="domain1/domain2/>
<allowed name="domain1"/>
<allowed name="domain2"/>
</protection>
Did someone faced with such issue?
I am sending a request to the server and server is sending a response back. But the response is in the form of sound file.
working - I am sending the request, response is received on the terminal, so I am writing it to text but later how to convert that into a sound file (.wav). The response from the server is as shown below.
The response as a text in the beginning and the binary in the middle and later text in the end. how to get a sound file out of it ? someone help me in this ?