'Unable to upload file with python request

I have a form i want to fill out that contains a file and a few fields.

If i do a post with just the fields values enters it works but it doesnt if i include the file.

Checking a successful post from firefox here is what i get.

https://snippet.host/knvy

my code

import requests
url = "https://localhost:8888/en/upload/upload.php"
cookie = { 'PHPSESSID': 'XXXXXX', 'id_remember' : 'XXXXXX,XXXXXX' }
open_file = open("File.txt", 'rb')


data = {
    'name': "Test Name",
    'fileToUpload' : open_file,
    'media_ref' : "https://www.testUrl.com/"
}


r = requests.post(url=url, data=data, cookies=cookie)
print(r)
print(r.content)

Form page code.

                <form id="uploadForm" action="/en/upload/upload.php" method="post" enctype="multipart/form-data">
                    <div id="feedback" style="display: none;" class="alert alert-danger"></div>
                    <!-- (file) -->
                    <div class="form-group">
                        <label for="exampleInputFile">Choose file (Max 2MB)</label>
                        <input type="file" class="form-control-file" name="fileToUpload" id="fileToUpload" onchange="updatefiletName()" >
                    </div>                  
                    <!-- (name) -->
                    <div class="form-group">
                        <label for="name">Name (Maximum characters: 160)</label>
                        <input type="text" class="form-control" id="name" name="name" value="" maxlength="160">
                    </div>
                    <!-- (Add media referance) -->
                    <div class="form-group">
                        <label for="media_ref">Media Reference URL (Optional)</label><small id="nameHelp" class="form-text text-muted"> Add IMDb or Steam URL to display metadata.</small>
                        <input type="text" class="form-control" id="media_ref" name="media_ref">
                        <small id="nameHelp" class="form-text text-muted">Examples: https://www.imdb.com/title/title</small>
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-angle-double-up"></i>
                            Upload
                        <i class="fas fa-angle-double-up"></i>
                    </button>
                </form>

Is there a way for me to find out what format the file to be uploaded should be in and how it should be added to the post. e.g should it be in data or payload or file in post?



Solution 1:[1]

So i figured out i need to pass on the name of the file along with the file itself and i need to pass it as a file not in the the data dict.

import requests

php_ses_id = "XXXXX"
mem_id = "XXXXX"

url = "https://localhost:8888/en/upload/upload.php"
cookie = { 'PHPSESSID': 'XXXXXX', 'id_remember' : 'XXXXXX,XXXXXX' }

txt_file = "test.txt"

mediainfo = open(media_info, 'r').read()

data = {
    'name': "test",
    'media_ref' : "https://www.testUrl.com/"
}

with open(txt_file, 'rb') as f:
    tfile = f.read()
    f.close()

# need to pass the name of the file along with the file itself
files = {
    'fileToUpload': ("Some-file.txt", tfile)
}
data['fileToUpload'] = tfile

r = requests.request("POST", url=url, data=data, files=files, cookies=cookie)
print(r)
print(r.content)
# success if it redirects to torrent dl page
print(r.url)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grimeire