I'm trying to decode a base64 pdf file and send it to another endpoint.
I used a python policy for the decoding part and here's the code
import base64
pdfB64 = flow.getVariable("request.content")
pdfFile = base64.b64decode(pdfB64)
flow.setVariable("pdfFileDecoded",pdfFile)
Now, when I send my http post request which is below
headers :
Accept : */*
boundary : --Boundaryy
--Boundaryy
Content-Disposition: form-data; name="testdu12janvier"; filename="testdu12janvier.pdf"
Content-Type: application/pdf
<< Heres is sensitive data which is basically a base64 encoded pdf file >>
--Boundaryy--
When I send this POST request and trace it in Apigee Edge, I notice that something else is encoded before the pdf file I think its either the boundary or one of the headers. This makes a corrupt pdf file which can't be read.
How do I isolate the pdf file from the request body without removing boundaries? as I'll need to send multiple in near future.
Related
I'm trying to send raw response (which is a jpeg image from my laptop camera) in python using flask, this is the particular snippet:
#app.route("/")
def stream():
frm = imencode('.jpg',cap.read()[1])[1].tobytes()
resp = Response()
resp.set_data(value="HTTP/1.1 200 Ok\nContent-Type:image/jpeg\nContent-Length:"+str(len(frm))+"\n\n"+str(frm))
return resp
My browser seem to display it as text nonetheless. If initialize
Response(frm.tobytes(), headers={"Content-Type":"image/jpeg"})
then then browser decodes the image ok. I'm not very good with web stuff, but from what I've found so far response consists of first line specifying the http version, response status code and respective message. Then come the header fields each on new line. then a clean line separates metadata from the body. I've read that the bare minimum for a simple response are Content-Type and Content-Length headers. Some sources also mention using \r in combination with \n to separate the lines but i didn't find the example so far and this source didn't specify where exactly \r should be added.
I have tested some requests inside the Postman app. First, I want to get the body information of an HTTP request inside Python (package requests used). The response appears positive with 200 OK.
response = session.request("POST", url, headers=headers, data=payload, verify ='custom-proxy-ca.crt')
Now I would like to get the body with
body = response.content
Print(body) delivers
b'\x83\x84\x01\x00\xc4\xff\xd4\xe9\xb4\xf6\xde,\x13\xa9\xc0(\xc7_\x8dL\x90\xf0\xb4K\xc4<\xe7\xb1M\x02)\xe0\x80z\xd0\xdf>\xcf\xd7\xd2\xec\x8d\x1e\xe4un\x0c\x83\xa1\x88g\xe7fah\x89\xbe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cc\xc0\xa5\xc8;bP\xdd\x9d\x1aeC\xfd\xf84\xa6\x14yG\xeb\xb5\x01\x03'
Now I try to search a token in the body, but it seems to be encrypted.
If I want to get the result of the JSON parser with
json.loads(body)
it returns
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 0: invalid start byte`.
Okay, it seems that the encoding is done in a different way than expected. But how did the Postman app do the decoding of the body? For example, I can read it there parsed as JSON (see the figure below). What am I doing wrong in Python?
Request
Okay, the problem is solved, but I want to share with you how to deal with this kind of problem.
The initial problem is to call the HTTP POST request with the header parameter Accept-Encoding like
'Accept-Encoding': 'gzip, deflate, br'
This line of code means: Locally can receive data in compressed format.
The server compresses the large file and sends it back to the client during processing. After receiving the IE, the IE performs a local pressure on the file.
The reason for the error is: the program did not extract the file
Solution: delete this line of code and it works
How do I send an mtom message in python? I have tried using the requests library but I am getting an error from the server. The workflow is HTTP headers with a SOAP envelope and a binary attachment. I am using an MTOM template file which is structured as follows:
Content-Type: multipart/related;
boundary=boundary1234567890; type="application/xop+xml";
start="<0.urn:uuid:1FACEDB95C3509148F1570480012346#w3.org>"; start-info="text/xml"
Transfer-Encoding: chunked
SOAPAction: "soap-action"
--boundary1234567890
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:1FACEDB95C3509148F1570480012346#w3.org>
set soap envelope here
--boundary1234567890
Content-Type: application/zip
Content-Transfer-Encoding: binary
Content-ID: <1.urn:uuid:1FACEDB95C3509148F1570480012347#w3.org>
set binary attachment here
I then send the message with the python requests library as follows:
import requests
data = open(template, 'rb').read()
response = requests.post(url, data=data, headers={})
However, I am getting an error from the server saying that my request was not able to be processed. I am probably doing something wrong, but I do not know how to send an MTOM message. Any help would be greatly appreciated.
I think the only real support for MTOM
is currently in Java and C#.You can use Axis2/C which I believe supports MTOM and
put a Python wrapper around it.
When we use flask function send_file, what is the actual encoding of the file sent on the REST body with mimetype image/jpeg? It is possible to send the file using a base64 enconding?
Line of code:
send_file("logo.jpg", mimetype='image/jpeg')
It's being sent as binary on the wire, e.g. the HTTP response will be
200 OK
Content-type: image/jpeg
Content-length: 1234
����JFIF��
ExifMM*
$���1�2��i��%�OnePlusONEPLUS A6013
etc.
You can manually base64 encode the data if your app requires it.
I've been trying to respond to serve images when someone goes to a url like localhost:5000/image.png. I'm serving the headers via python sockets
Steps I tried:
When the file is requested, I first loaded the raw binaries of the file as shown below.
data=open(filename, "rb").read()
After loading the binaries, I formatted it into the headers as shown below
HTTP/1.1 200 OK
Content-Type: image/png
Content-Length: {len(data)}
{data}
When I return the response, the browser displays a small white box.
After trying several solutions I found online(mentioned below btw), I concluded that the white box only appears if the data I'm returning in the response is invalid
Things I've tried:
Encoding the data variable using base64 encode, same result
Attempting to decode the data using utf-8, raises "Invalid Start Byte"
Attempting to decode the data using ISO-8859-1, it did decode but it displays the same white box
I've tried the same thing with non image files with their respective content-types, they work perfectly
Source code of the part
class ClassicFileRequest(object):
#staticmethod
def create(response, rtp):
basestring=f"HTTP/1.1 200 OK\nContent-Type: {rtp}\nContent-Length: {len(response)}\n\n{response}" # Base string, this is where all the file data is formatted
return basestring
def responsefromfile(filename):
print(filename)
ft=mimetypes.guess_type(filename)[0]
if filename.endswith((".webp",".jpeg",".png",".gif",".ico")):
data=open(filename, "rb").read() # Read binaries for image files
return ClassicFileRequest.create(data, ft)
else:
data=open(filename, "r").read()
return ClassicFileRequest.create(data,ft)
UPDATE:*
The error has been fixed, apparently formatting a bytes object into a string wasn't a great thing to do