I have to convert js code to python. The js code performs a file upload via a POST request, using fetch(). This is the js code:
<input type="file" />
<button onclick="upload()">Upload data</button>
<script>
          upload = async() =>
          {             
             const fileField = document.querySelector('input[type="file"]');
             await uploadDoc(fileField.files[0] );
          };
          
          uploadDoc = async( file ) =>
          {             
              let fd = new FormData();
              fd.append( 'file', file ); 
              fd.append( 'descr', 'demo_upload' );
              fd.append( 'title', name );
              fd.append( 'contentType', 'text' );
              fd.append( 'editor', user );
              let resp = await fetch( url, { method: 'POST', mode: 'cors', body: fd }); 
          };
</script>
The code works and complies with the fetch() docs, provided here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#uploading_a_file
Now when i try to recreate this in python, i get a 500 HTTP Status Code This is the python code:
from urllib import request
from urllib.parse import urlencode
import json
with open('README.md', 'rb') as f:
          upload_credentials = {
            "file": f,
            "descr": "testing",
            "title": "READMEE.md",
            "contentType": "text",
            "editor": username,
          }
          url_for_upload = "" #here you place the upload URL
          
          req = request.Request(url_for_upload, method="POST")
          form_data = urlencode(upload_credentials)
          form_data = form_data.encode()
    
          response = request.urlopen(req, data=form_data)
          http_status_code = response.getcode()
          content = response.read()
          print(http_status_code)
          print(content)
This doesn't work however and i get this error:
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500:
Someone with both js and python experience might be able to see what is wrong in the python side, or how to convert the fetch() function to python.
 
    