9
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?

GuySome
  • 91
  • 1
  • 1
  • 2

2 Answers2

6

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)
cieunteung
  • 1,725
  • 13
  • 16
0

python requests - POST Multipart/form-data without filename in HTTP request See this solution for the reason why the None has been added in the Tuple. It's mainly because you need to make "file_name" to None.

  • 1
    Since this doesn't answer the question at hand, this would have been better if this was written as a comment instead of a answer – Wasi Master Feb 03 '22 at 06:55