Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set boundary of Content-type using python requests?

POST /write.php HTTP/1.1
HOST: upload.sitename.com
accept-encoding: gzip, deflate
content-length: 788
content-type: multipart/form-data; boundary=----WebKitFormBoundarycHb9L3gZr8gzENfz
referer: http://www.sitename.com
user-agent: sitename.app

------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="app_id";

123123123123
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="id";

idididid
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="mode";

write
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="subject";

subject
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="user_id";

456456456456
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="memo_block[0]";

memoblock
------WebKitFormBoundarycHb9L3gZr8gzENfz--

I want to send this request using python.

import requests
from email.mime.multipart import MIMEMultipart

related = MIMEMultipart('form-data','----WebKitFormBoundary6o6EZLygJtLbQhjb')

headers = dict(related.items())
headers['User-Agent'] = 'sitename.app'
headers['referer'] = 'http://www.sitename.com'

files = {
    'app_id':(None, '123123'),
    'id':(None, 'idid'),
    'mode':(None, 'write'),
    'subject':(None, 'subject'),
    'user_id':(None, '456456'),
    'memo_block[0]':(None, 'memoblock')
}

req = requests.post('http://upload.sitename.com/_app_write_api.php', files=files, headers=headers)
print(req.status_code)

So, I wrote code like this.

I identified request using Wire Shark, but it is not that I want.

POST /_app_write_api.php HTTP/1.1
Host: upload.sitename.com
User-Agent: sitename.app
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Type: multipart/form-data; boundary="----WebKitFormBoundary6o6EZLygJtLbQhjb"
MIME-Version: 1.0
referer: http://www.sitename.com
Content-Length: 680

--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="app_id"

123123
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="id"

idid
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="mode"

write
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="subject"

subject
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="user_id"

456456
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="memo_block[0]"    

memoblock
--e68a0877353c411b8972ec5f868b2e41--

actually send request like this. And server send response 'error' to me.

I think that it occur because '--e68a0877353c411b8972ec5f868b2e41'.

How can I set this? or can I set boundary randomly?

like image 271
GuySome Avatar asked Jul 10 '18 11:07

GuySome


People also ask

What is boundary in post request?

The boundary acts like a marker of each chunk of name/value pairs passed when a form gets submitted. The boundary is automatically added to a content-type of a request header.

What is request header boundary?

The boundary is included to separate name/value pair in the multipart/form-data . The boundary parameter acts like a marker for each pair of name and value in the multipart/form-data. The boundary parameter is automatically added to the Content-Type in the http (Hyper Text Transfer Protocol) request header.

What is request content in Python?

Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc.


1 Answers

It is because header value ----WebKitFormBoundary6o6EZLygJtLbQhjb and body --e68a0877353c411b8972ec5f868b2e41 is not match. to send multipart/form-data you don't neet to set header Content-Type

import requests

headers = {
    'User-Agent' : 'sitename.app',
    'referer' : 'http://www.sitename.com'
}

files = {
    'app_id':(None, '123123'),
    'id':(None, 'idid'),
    'mode':(None, 'write'),
    'subject':(None, 'subject'),
    'user_id':(None, '456456'),
    'memo_block[0]':(None, 'memoblock')
}

req = requests.post('http://upload.sitename.com/_app_write_api.php', files=files, headers=headers)
print(req.status_code)
like image 150
cieunteung Avatar answered Sep 30 '22 09:09

cieunteung