Project

General

Profile

API Rest - Image attachments broken

Added by Federico Vergelli over 2 years ago

Hello folks, this is the issue I'm having right now:

Summary: Whenever I try to upload an image file (whether is png or jpg) through the Rest service, it shows itself broken. It happens only with the images, the rest uploads just fine.

Environment:
- Redmine 4.2.1 on Docker container
- Requests made by Python's library (request) [Python: 3.9]

Behaviour:
I've made a couple of functions to manage a massive file upload to the server. I make these requests from a Python Notebook. These functions upload an attachment to an existing issue, so that's why I make use of the PUT method, as Redmine docs say it's supported. When I go to the UI the image is shown broken, and the browser console shows Failed to load resource: the server responded with a status of 404 (Not Found) for each image file there was attached to this issue.

Steps to reproduce:

My requests look something like this:

            .
            .
            .

            # Opening the file
            file_n = {'file': open( 'files\{}'.format( file),'rb')}

            # The API call to upload the file
            response = requests.post( url_objective + '/uploads.json?filename={}'.format( file), 
                                files=file_n, headers={"Content-Type":"application/octet-stream"},
                                verify=False, auth=(  conections_objetivo["user"], conections_objetivo["password"]))

            # Very self explanatory
            if str( response) == "<Response [201]>":

                # Variabilizing the token 
                token = response.json()["upload"]["token"]

                # the body of the PUT method
                body = {
                "issue": {
                    "uploads": [
                    {"token":"{}".format( token), "filename":"{}".format( file), "content_type":"{}".format( content_type_formater( file))}
                               ]
                         }
                       }
                # The PUT method
                response = requests.put( url_objetivo + '/issues/{}.json'.format( issue_id_finder( issue)), json=body, headers={"Content-Type":"application/json"},
                                            verify=False, auth=(  conections_objetivo["user"], conections_objetivo["password"]))

Where:

  • file is the file in string type
  • content_type_formater( file) is a well-defined function that simply returns
    "png": "image/png",
    "jpg": "image/jpeg",
    "txt": "text/plain"
    depending the case.

Any advice?