0
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 = '------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="method"\r\n\r\nregular\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="source"\r\n\r\nDrag & Drop\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="course_id"\r\n\r\n10122614\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="school_id"\r\n\r\n2658\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="is_mandatory_tagging"\r\n\r\ntrue\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="data"\r\n\r\n{"submitted":true,"document_submission_id":30234005}\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="files"; filename="screencapture-us13-proxysite-process-php-2022-04-19-18_33_18.png"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="hash"\r\n\r\nb9290fc1a94abb6a1706f57a3a2b254119fe248f\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="upload_application_name"\r\n\r\nflu\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY\r\nContent-Disposition: form-data; name="upload_application_version"\r\n\r\nupld_flu_breakpoint_test_v4_0\r\n------WebKitFormBoundaryMxmgytoOe4GjAGyY--\r\n'
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 add documents 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 }'
}

edit 2: I looked at the other question before I posted thi squestion but it doesn't seem to fix the issue, unless I am doing something wrong

trin cjon
  • 29
  • 3
  • The error message is unrelated to `files`. It complaints about `data` which should be a dictionary. – Klaus D. Apr 25 '22 at 02:04
  • i edited my post with the dicitionary, but I still gett an error in the HTML and no file has been uploaded – trin cjon Apr 25 '22 at 02:09
  • Does this answer your question? [ValueError: Data must not be a string](https://stackoverflow.com/questions/27553082/valueerror-data-must-not-be-a-string) – Cristik Apr 25 '22 at 05:16
  • not really i saw that question before but it didn't fix my issue – trin cjon Apr 25 '22 at 16:28

0 Answers0