Search code examples
pythonheader-filesmechanize

Structure of HTML Form Data


I know this is stupid question but how does payload of this form data be formatted. I can't make request more than one time so i can't see real request send.

I have tried same on local html file and then tries to make request but i don't know if image attachment will be right or not.

<form method="post" id="txnForm" class="form-horizontal" enctype="multipart/form-data" autocomplete="off" action="https://example.com"> 

<div class="col-md-12">
                                    <div class="col-md-3">
                                        <label>Name : </label><br>
                                        <input type="text" class="form-control" name="name[]" required="" />
                                    </div>
                                    <div class="col-md-6">
                                        <label>Address : </label><br>
                                        <input type="text" class="form-control" name="address[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Occupation : </label><br>
                                        <input type="text" class="form-control" name="occupation[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Mobile : </label><br>
                                        <input type="text" class="form-control" name="mobile[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Email : </label><br>
                                        <input type="text" class="form-control" name="email[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Aadhar No: </label><br>
                                        <input type="text" class="form-control" name="aadhar_no[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Aadhar Image: </label><br>
                                        <input type="file" class="form-control" name="aadhar_image" required="" />
                                    </div>
                                </div>
                          

                                <div class="col-md-12">
                                    <div class="col-md-3">
                                        <label>Name : </label><br>
                                        <input type="text" class="form-control" name="name[]" required="" />
                                    </div>
                                    <div class="col-md-6">
                                        <label>Address : </label><br>
                                        <input type="text" class="form-control" name="address[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Occupation : </label><br>
                                        <input type="text" class="form-control" name="occupation[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Mobile : </label><br>
                                        <input type="text" class="form-control" name="mobile[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Email : </label><br>
                                        <input type="text" class="form-control" name="email[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Aadhar No: </label><br>
                                        <input type="text" class="form-control" name="aadhar_no[]" required="" />
                                    </div>
                                    <div class="col-md-3">
                                        <label>Aadhar Image: </label><br>
                                        <input type="file" class="form-control" name="aadhar_image" required="" />
                                    </div>
                                </div>
                                <div class="clearfix">&nbsp;</div>
</form>

If I'm right then form data will look like this:

        formData = {
            'aadhar_no[]' : ['123456789', '123456789', '123456789', '123456789'],
            'address[]' : [],
            'csrf_token' : csrf_token,
            'email[]' : [],
            'member_id' : member_id,
            'mobile[]' : [],
            'name[]' : [],
            'occupation[]' : [],
            'aadhar_image' : []
        }

Solution

  • HTML form data is never sent via Javascript. The two primary types are multipart/form-data, which is required when you have a file field, and application/x-www-form-urlencoded, which is the default, and is much more compact. multipart/form-data is sent like a MIME-encoded email:

    Your data will be sent like this. The spec is in RFC 7578: https://www.rfc-editor.org/rfc/rfc7578

    Content-Type: multipart/form-data, boundary=---1234
    
    ---1234
    Content-Disposition: form-data; name="name[]"
    
    value-of-name-field
    ---1234 
    Content-Disposition: form-data; name="address[]"
    
    value-of-address-field
    ---1234 
    Content-Disposition: form-data; name="occupation[]"
    
    value-of-occupation-field
    ---1234 
    ......
    Content-Disposition: form-data; name="aadhar_image[]"
    Content-Type: multipart/mixed; boundary=---5678
    
    --5678
    Content-Disposition: file; filename="image.jpg"
    Content-Type: image/jpg
    Content-Transfer-Encoding: binary
    
    .... jpeg image data ...
    --5678
    --1234
    

    Many of the web frameworks will convert this to JSON for you, but that's not how it crosses the wire.