Python remove multipart header content - python

When I try to upload a CSV file and read them, the header content is also added to the csv at the top and at the end of the file :
----------------------------1323424324242342
Content-Disposition: form-data; name="file"; filename="test.csv"
Content-Type: text/csv
<actual content here>
----------------------------113131313331313133--
How do I get the actual content in the file and ignore the multipart headers?

Related

Http request boundaries getting caught in pdf decoding process

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.

POST request with sent form data does not return correct response

I´m trying to log in to a website using my account. In python code, I use:
login_response = session.post('mysite.com', login_data, cookies=session.cookies)
where session is a requests session object, the cookies are exactly the cookies that are also sent when I try the same in a browser (it´s two specific cookies that are sent by the browser as part of the request when I log in), and login_data is a dictionary of form data that is sent in the post request.
However, the expected response is: code 303, a redirect link is sent, and two new cookies (that are the use login session). This is what happens in the browser.
When I want to do this with the code above, I get instead code 200, no redirect link, and no cookies. It´s as if the site just 'reloads'. The form data itself (password + user) is correct.
I read the request content from my browser:
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="login__standard_submitted"
1
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="csrfKey"
2fcc7af2aa54f0e3ab85a6bdeca26f98
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="ref"
aHR0cHM6Ly9jcmltZW1hcmtldC53by8=
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="MAX_FILE_SIZE"
20971520
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="plupload"
92ee7fd8f0a2249c2327184c5b95d6c9
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="auth"
myusername
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="password"
password
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="remember_me"
0
-----------------------------366521339228594542373308740035
Content-Disposition: form-data; name="remember_me_checkbox"
1
-----------------------------366521339228594542373308740035--
The data I get from this, I just wrote into a dictionary of key: values, e.g.
'auth': 'myusername'
Again a tl;dr:
I send the right cookies and what I observed as seemingly correct data when performing the POST request in code. However, I get an entirely different result than in the browser, and fail to log in.
Did I misunderstand something? What do I need to change to log in by code?
EDIT: In the headers, I also set 'Content-Type' to 'multipart/form-data', just as in the browser.
EDIT 2: Instead if I set login_data with files=, it still doesn´t work.
From what I have seen you have to use files= to send a multipart/form-data request using request.post however you have to use tuples and set the filename to NOne so they are not sent as files.
login_data = (
('login__standard_submitted', (None, '1')),
('csrfKey', (None, '2fcc7af2aa54f0e3ab85a6bdeca26f98')),
('ref', (None, 'aHR0cHM6Ly9jcmltZW1hcmtldC53by8')),
...
)
login_response = session.post('mysite.com', files=login_data, cookies=session.cookies)
I wonder if your session object is sending an http: request and should instead send a https: request. This could lead to a redirect with 303 response. The "requests" package seems to handle this type of response.
import requests
res = requests.get('http://google.com')
print(res.url)
res = requests.get('http://stackoverflow.com')
print(res.url)
The output to those above are
http://www.google.com/
https://stackoverflow.com/

Strange behaviour of request.POST in Django

I'm sending a POST request to a Django server through Postman. This is what the body of my request looks like
POST /update/ HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 0764e56c-0fd8-fcce-5248-34f7d05f2748
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="UploadDownloadSettings"
dssssss
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="settings";
filename="settings.zip"
Content-Type: application/zip
When I try to access request.POST['UploadDownloadSettings'], the program says the key is not valid.
When I loop through the keys in request.POST I get a bunch of keys from the zip file and the key name.
According to the docs, this should parse out form-data. This seems like standard form data as far as I understand.
https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.POST
Am I misunderstanding the way the django post request works?
Output of request.POST
Key:
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition
Value:
form-data
Key:
name
Value:
"UploadDownloadSettings"
Key:
//actual filenames from the zip with their value
Value:
Blank
Output of request.FILES: blank map
Output of request.content_type: application/x-www-form-urlencoded

Unable to open mht file in IE

I am trying to extract the attachments from an email mht file and write to separte mht files.I used the code in this link Extracting mht files to extract and write the attachments to a file.
Apparently I am unable to open the output mht files in the case of Content/Type: application/pdf and application/octet-stream. Below is the format of the output mht file which I am not able to open in IE. I am wondering why I am unable to open the file in IE. Any help or suggestions would be useful. Thanks.
MIME-Version: 1.0
Content-Type: multipart/related; type="text/html";
boundary="===============0805110039=="
--===============0805110039==
Content-Type: application/pdf
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <abcd.pdf>
<Payload>
--===============0805110039==--

Parse HTTPRequest Body from multipar/form in python

I receive a Response from the server with the next body:
body='------WebKitFormBoundarylY6hpxLHtLTD33AY\r\nContent-Disposition: form-data; name="file"; filename="language.py"\r\nContent-Type: text/x-python-script\r\n\r\n#!/usr/bin/env python\n
.....
.....
\r\n------WebKitFormBoundarylY6hpxLHtLTD33AY--\r\n'
And I want to parse this body and extract, name, filename, content-type and the full content of the file for storing.
May Be possible?
Thanks in advance.
Tornado should parse this for you; the contents will be available in self.request.files.
http://www.tornadoweb.org/en/stable/httpserver.html#tornado.httpserver.HTTPRequest.files

Categories

Resources