Tuesday, July 31, 2018

Python - multipart/form-data post requests

multipart/form-data contains boundary to separate name/value pairs. 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.
The form with enctype="multipart/form-data" attribute will have a request header Content-Type : multipart/form-data; boundary --- WebKit193844043-h (browser generated vaue).
The payload passed looks something like this:
Content-Type: multipart/form-data; boundary=—-WebKitFormBoundary7MA4YWxkTrZu0gW

    --—-WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name=”file”; filename=”captcha
    Content-Type:

    --—-WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name=”action

    submit
    --—-WebKitFormBoundary7MA4YWxkTrZu0gW--
e.g.

from requests_toolbelt import MultipartEncoder
......

params = {
    "__EVENTVALIDATION": event_validation,
    "__VIEWSTATE": view_state,
    "MainContent_ScriptManager1_HiddenField": decode_content,
    "AutoCompleteCASNumbers": auto_CASnumber,
    "__VIEWSTATEGENERATOR": view_stategenerator,
    "ctl00$MainContent$cboHydSub": 'Submitted',
    "ctl00$MainContent$ddlRangeType": "Between",
    "ctl00$MainContent$tbRangeStartDate": fromDate,
    "ctl00$MainContent$tbRangeEndDate": toDate,
......}
url = 'https://fracfocusdata.org/DisclosureSearch/Search.aspx'
m = MultipartEncoder(params)
headers['Content-Type'] = m.content_type
resp = requests.post(url,headers=headers,data=m)
........



Reference:

https://www.jianshu.com/p/902452189ca9

No comments:

Post a Comment