pythonmultipartform-dataurllib2poster

Python multipart file uploading with poster - set the file's name that is being sent


I try uploading a file using the following code (which has been summarized).

On the server-side, when I check the name of the received file, I see that it is set to the file_path parameter.

Currently, the poster library takes the file_path parameter that I entered (e.g.: file_path=~/user/data.csv), and sends it as the file’s name during the file multipart uploading.

I am looking for a way to change the file's name that is being sent (Since the server expects a file name without the / char)

Thanks in advance.

P.S. I have already tried adding the "name" key to the values, but it didn't help.

values = {'file': open(file_path, 'rb')}

poster.streaminghttp.register_openers()

datagen, headers = poster.encode.multipart_encode(values)

# Create the Request object
request = urllib2.Request(address, datagen, headers)

Solution

  • I have managed to solve this issue using the following code:

        f = open(file_path, "rb")
        mp = MultipartParam("file", fileobj=f, filename="file.csv")
    
        values = {"file": mp,
                  constants.AUTHORIZATION_HEADER_KEY: constants.AUTHORIZATION_HEADER_VALUE % self._token,
                  constants.CACHE_CONTROL_HEADER_KEY: constants.CACHE_CONTROL_NEGATIVE_VALUE
                  }
    
        poster.streaminghttp.register_openers()
    
        datagen, headers = poster.encode.multipart_encode(values)
    
        # Create the Request object
        request = urllib2.Request(address, datagen, headers)