how to use python requests to upload file?

import requests

cookies = {
    'root_session_id': 'something',
    'visid_incap_987752': 'something',
  
    '_gid': 'something',
     #there are more cookies but I dont wanna post all of them for safety reasons and I changed 
     #the values for the above cookies, I changed to something for safety reasons for stackoverflow, however they 
     #are proper values
 
}

headers = {
    'authority': 'www.coursehero.com',
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'en-US,en;q=0.9',
    'cache-control': 'no-cache',
    'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarykw7irLeGtukpiiD6',
    # Requests sorts cookies= alphabetically

    'origin': 'https://www.coursehero.com',
    'pragma': 'no-cache',
    'referer': 'https://www.coursehero.com/upload/',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
    'x-newrelic-id': 'Ug8CUVVbGwIDUlVUBgkGVg==',
}

data="------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="method"rnrnregularrn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="source"rnrnDrag & Droprn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="course_id"rnrn10122614rn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="school_id"rnrn2658rn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="is_mandatory_tagging"rnrntruern------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="data"rnrn{"submitted":true,"document_submission_id":30234005}rn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="files"; filename="screencapture-us13-proxysite-process-php-2022-04-19-18_33_18.png"rnContent-Type: image/pngrnrnrn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="hash"rnrnb9290fc1a94abb6a1706f57a3a2b254119fe248frn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="upload_application_name"rnrnflurn------WebKitFormBoundaryMxmgytoOe4GjAGyYrnContent-Disposition: form-data; name="upload_application_version"rnrnupld_flu_breakpoint_test_v4_0rn------WebKitFormBoundaryMxmgytoOe4GjAGyY--rn"
files = {

'file': open('testfile.txt', 'rb')}
  

response = requests.post('https://www.coursehero.com/api/v1/uploads/', cookies=cookies,   files=files,  headers=headers, data=data )
with open('b.html',"w") as f:
      f.write(response.text) 

error I get is data cant be a string

Traceback (most recent call last):
  File "main.py", line 41, in <module>
    response = requests.post('https://www.coursehero.com/api/v1/uploads/', cookies=cookies,  files=files, headers=headers,  data=data )
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/sessions.py", line 443, in prepare_request
    p.prepare(
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/models.py", line 321, in prepare
    self.prepare_body(data, files, json)
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/models.py", line 514, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/home/runner/WorstRecentLaw/venv/lib/python3.8/site-packages/requests/models.py", line 124, in _encode_files
    raise ValueError("Data must not be a string.")
ValueError: Data must not be a string.

https://www.coursehero.com/upload/ is the website you can take and look and see the requests when I click on upload file or when I try to drag and drop a file. u can log in using this account email: stackkkyoverflow@hotmail.com password: stackkkyoverflow and then go to the website https://www.coursehero.com/upload/ or you can easily create a free account (don’t put your credit card or anything, no need for that) just create a free account and go to the upload website above incase someone changes the password.
I want to upload a file using python requests to that website, idk if I have the correct request as well. Also I copied the network request using cmd bash and converted it to python requests using the online converter so I don’t miss out on anything

edit1: even when I use the dictionary below for data instead of the string, it does not upload the file, it just says error occurred in the html

data =  {
'method': 'regular',
'source': 'Desktop',
'course_id': '10122614',
'school_id': '2658',
'is_mandatory_tagging': 'true',
'data': '{"submitted":true,"document_submission_id":30237700}',
'files': '(binary)',
'hash': 'b929vef22hucfc1a94abxasq6a1706f57a3332uduw2b254119fe248f',
'upload_application_name': 'flu',
'upload_application_version': 'upld_flu_breakpoint_test_v4_0 }'
}

Leave a Comment